I want to create a page which user can modify data on that page. So I tried to use a ajax call to replace the original data table row into a form. The new form could be created and replace the row by my code currently. But after edited data in that form and click 'Update', nothing happened. In my console shows something like:
ActionController::RoutingError (No route matches "/projects/5"):
I can't figure out why it didn't work.
Codes are shown as following:
index.html.erb
<tr>
<th>Name</th>
<th></th>
<th></th>
</tr>
<% @projects.each do |project| %>
<tr id="project_<%= project.id %>">
<td><%= link_to project.name, project %></td>
<td><%= link_to 'Edit', edit_project_path(project), :method => :get, :remote => true %></td>
<td><%= link_to 'Destroy', project, :confirm => 'Are you sure?', :method => :delete , :remote => true %></td>
</tr>
<% end %>
edit.js.erb
$('#project_<%= @project.id%>').replaceWith("<%= escape_javascript(render :partial=>'edit') %>");
_edit.html.erb
<tr id="project_<%= @project.id%>">
<%= form_for(@project, :remote => true) do |f| %>
<%= form_authenticity_token %>
<td><%= f.text_field :name %></td>
&l开发者_JS百科t;td><%= f.submit 'Update' %></td>
<td><%= link_to 'Destroy', @project, :confirm => 'Are you sure?', :method => :delete , :remote => true %></td>
<% end %>
</tr>
Routing errors are usually caused by one of the following:
you don't have the route specified in routes.rb. Run "rake routes" at the command line and see if something like "/projects/:id" shows up. If not you have to add the route by adding a projects resource or otherwise specifying the route manually. Make sure the HTTP method matches the HTTP verb specified in routes.rb.
you have no controller named named ProjectsController or no create method (if your ajax method is posting) or no update method (if its putting).
精彩评论