Rails - updating div w / Ajax and: remote => true

I followed this Railscast when submitting the form via Ajax and updating the div without reloading the page, but I have a problem with one part of it.

Ryan has $("#products").html("<%= escape_javascript(render(@products)) %>"); in the index.js.erb file to update the #products div when submitting the form. I'm trying to figure out if an instance variable, like @products for my situation, or can I just replace it with a url.

I'm trying to do the same thing as Ryan in this screencast, but instead of displaying the 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 %> 

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 this clearly does not work. Should I use a variable here instead of _duedate ? And if so, how would I install this in the controller? I mean, what does a variable represent?

Also, for what it's worth, partial rendering is correct and displaying the value of todo.due ... it just doesn't update when the form is todo.due .

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 
+4
source share
2 answers

try it

in your controller action (say sample_action)

 def sample_action @todos = #your code respond_to do |format| format.js end end 

and you have sample_action.js.erb

 $("#dueon").html("<%= raw escape_javascript(render(:partial => 'duedate')) %>") 

then inside partial, you have access to the new @todos instance @todos

NTN

+5
source

I will answer you separately, because I think that your whole setup should be insignificant (IMO, this may not be so)

I think you should have a todos controller with a project that belongs to it,

--- Models ----------------

 class Project < ActiveRecord::Base has_many :todos end class Todo < ActiveRecord::Base belongs_to :project end 

---- routes ---------------

 resources :projects do resources :todos do end end 

---- controllers ----------

class ProjectsController <ApplicationController

end

 class TodosController < ApplicationController def new @project = Project.find(params[:project_id]) @todos = @project.todos.build end end 

in your view (views / todos.html.erb)

 <%= @project.name %> <%= form_for([@Project, @todos]) do |f| %> #todo form code <% end%> 

In accordance with the attitude, the project has a lot of todos, it is always clear to show the details of the project on the add todo screen, and not allow users to add new todos from the project screen.

and again, this is my personal opinion, feel free to ask any questions :)

0
source

All Articles