Rails 3: Simple AJAXy Page updates?

前端 未结 6 2051
谎友^
谎友^ 2020-12-14 11:29

I can\'t believe I\'ve been looking four hours for this simple task, but I have.

In Rails 2.3, I could replace one section of a page with this simple code:

6条回答
  •  遥遥无期
    2020-12-14 12:01

    As far as I know, along the same line as the answer above, you can do something like this in your template:

    <%= link_to "Update User List", @reload_users_path, :remote => true %>
    

    And in controller, do this:

    respond_to do |format|
      format.js {
        render :text => "alert('reloaded')"
      }
    end
    

    This way you can have controller "execute" client side JS much the same as as render :update used to do. This is equivalent to doing the following in Rails 2:

    render :update do |page|
       page << "alert('reloaded')"
    end
    

    Is there any reason why this approach is not advisable?

提交回复
热议问题