Rails - updating div w/ Ajax and :remote => true
ajax, ruby-on-rails, ruby-on-rails-3
Solution
Try this
in you controller action , (say sample_action)
def sample_action
@todos = #your code
respond_to do |format|
format.js
end
end
and you have a `sample_action.js.erb`
$("#dueon").html("<%= raw escape_javascript(render(:partial => 'duedate')) %>")
then inside the partial, you have access to the new `@todos` instance variable
HTH
Problem
I've followed this Railscast on submitting a form via Ajax and updating a div without reloading the page, but I'm having trouble with one portion of it. Ryan has `$("#products").html("<%= escape_javascript(render(@products)) %>");` in an `index.js.erb` file to update the `#products` div when the form is submitted. I'm struggling to understand whether an instance variable like `@products` is relevant for my situation, or if I can simply substitute it for a URL. I'm trying to do the same thing as Ryan in this screencast, but instead of display search results I just want to display the updated value. In show.html.erb I have: ``` <% @project.project_todos.order("created_at DESC").where(:status => false).each do |todo|%> <%= form_for todo, :remote => true, :"data-replace" => "#dueon" do |f| %> <%= f.text_field :due %> <%= f.submit :class => "primary", :value => "Add" %> <% end %> <div id="dueon"> <%= render "duedate", :todo => todo %> </div> <% end %> ``` The partial `_duedate.html.erb` has one line in it: `<%= todo.due %>` So in my index.js.erb I currently have this: `$("#dueon").html("<%= escape_javascript(render("_duedate")) %>");` but it's clearly not working. Do I have to use a variable here in place of the `_duedate`? And if so, how would I set this up in the controller? I mean what does the variable have represent? Also, for what it's worth, the partial is rendering correctly and displaying the `todo.due` value...it's just not updating when I submit the form. ProjectsController: ``` def show @project = Project.find(params[:id]) # Display the form to create a new todo @project_todo = ProjectTodo.new respond_to do |format| format.html # show.html.erb format.json { render :json => @project } end end ```