Rails 3.2,没有路由匹配{:controller =>'xxx':action =>'xxx' } [英] Rails 3.2, No Route Matches { :controller=>'xxx' :action=>'xxx" }
问题描述
我正在尝试使用复选框形式编辑多个记录,如此Railscast 。从那时起,Rails发生了变化,我在路由和表单方面遇到了麻烦。
I'm trying to edit multiple records with a checkbox form as shown in this Railscast. Rails has changed since then and I'm having trouble with routing and forms.
我有一个PeopleController,我正在尝试制作一个可以编辑所有表单的表单一次拨打电话号码。是的,我知道这不是一个有用的功能,因为通常没有理由将每个人的电话号码都更改为相同的东西,但是我只是用它来使代码适应-而且不起作用!
I have a PeopleController and I'm trying to make a form that will edit all of their phone numbers at once. Yes, I know this isn't a useful function, as there is normally no reason to change everyone's phone numbers to the same thing, but I'm just using this to get comfortable with the code - and it isn't working!
这就是我所拥有的:
people_controller:
people_controller:
class PeopleController < ApplicationController
helper_method :sort_column, :sort_direction
def index
@people = Person.order(sort_column + " " + sort_direction)
respond_to do |format|
format.html # index.html.erb
format.json { render json: @people }
end
end
# GET /people/1
# GET /people/1.json
def show
@person = Person.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.json { render json: @person }
end
end
# GET /people/new
# GET /people/new.json
def new
@person = Person.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @person }
end
end
# GET /people/1/edit
def edit
@person = Person.find(params[:id])
end
# POST /people
# POST /people.json
def create
...
end
# PUT /people/1
# PUT /people/1.json
def update
...
end
def update_multiple
@people = Person.find(params[:people_ids])
@people.each do |person|
person.update_attributes!(params[:person].reject { |k,v| v.blank? })
end
flash[:notice] = "Updated people"
redirect_to people_path
end
# DELETE /people/1
# DELETE /people/1.json
def destroy
...
end
private
def sort_column
Person.column_names.include?(params[:sort]) ? params[:sort] : "name"
end
def sort_direction
%w[asc desc].include?(params[:direction]) ? params[:direction] : "asc"
end
end
index.html.erb:
index.html.erb:
<h1>Listing people</h1>
<% form_tag edit_multiple_people_path do %>
<table>
<tr>
<th></th>
<th><%= sortable "name" %></th>
<th><%= sortable "phone" %></th>
<th><%= sortable "created_at" %></th>
<th></th>
<th></th>
<th></th>
</tr>
<% @people.each do |person| %>
<tr>
<td><%= check_box_tag "people_ids[]", person.id %></td>
<td><%= person.name %></td>
<td><%= person.phone %></td>
<td><%= person.created_at %></td>
<td><%= link_to 'Show', person %></td>
<td><%= link_to 'Edit', edit_person_path(person) %></td>
<td><%= link_to 'Destroy', person, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
<% # submit_tag "Edit Checked" %>
<% end %>
<br />
<%= link_to 'New Person', new_person_path %>
edit_multiple.html.erb:
edit_multiple.html.erb:
<%= form_for :person, :url => update_multiple_path, :html => { :method => :put } do |f| %>
<ul>
<% @people.each %>
<li>
<%= hidden_field_tag "people_ids[]", person.id %>
<%=h person.name %>
</li>
<% end %>
</ul>
<p>
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</p>
<p><%= f.submit "Submit" %></p>
<% end %>
Routes.rb:
Routes.rb:
resources :people do
collection do
put 'update_multiple'
end
end
耙路:
update_multiple_people PUT /people/update_multiple(.:format) people#update_multiple
people GET /people(.:format) people#index
POST /people(.:format) people#create
new_person GET /people/new(.:format) people#new
edit_person GET /people/:id/edit(.:format) people#edit
person GET /people/:id(.:format) people#show
PUT /people/:id(.:format) people#update
DELETE /people/:id(.:format) people#destroy
编辑:
我的路线w搞砸了。它们现在可以正常工作,如上所示。
My routes were messed up. They are now working properly as shown above.
推荐答案
您正在混合单数和复数资源。 edit_multiple_path
路由当前配置为单一资源,这意味着它希望将单个 Person
作为参数传递。但是,根据定义,您要尝试一次更新多个人–在这种情况下,使用单一路线没有意义。
You're conflating singular and plural resources. The edit_multiple_path
route is currently configured as a singular resource, meaning that it expects a single Person
to be passed as an argument. However, by definition, you're trying to update multiple people at a single time – it doesn't make sense to use a singular route, in this case.
,请尝试使用收藏路线:
# config/routes.rb
resources :people
collection do
post 'update_multiple/:people_ids', :action => 'update_multiple'
end
end
这将使以下路线可用:
This will make the following route available:
update_multiple_invites POST /invites/update_multiple/:people_ids(.:format) invites#update_multiple
编辑:
在索引视图中,您需要输出表单以便显示它们:
In the index view, you'll need to output your forms in order for them to be displayed:
# app/views/people/index.html.erb
<%= form_tag edit_multiple_people_path do %>
# app/views/people/_edit_multiple.html.erb
<%= form_for :person, :url => update_multiple_people_path, :html => { :method => :put } do |f| %>
请注意,<%%>
解释包含在其中的所有内容,但不输出。您需要使用<%=%>
才能实际输出表单的内容。
Note that <% %>
interprets everything enclosed within, but does not output it. You need to use <%= %>
in order to actually output the form's contents.
此外,不要忘记,您的部分需要扩展名 .erb
才能解释Ruby代码。也许您已经相应地对其进行了命名,但是您的帖子描述了 edit_multiple.html
无 .erb
。
Also, don't forget that your partial needs the extension .erb
in order for it to interpret Ruby code. Perhaps you've named it accordingly, but your post depicts the name edit_multiple.html
without the .erb
.
这篇关于Rails 3.2,没有路由匹配{:controller =>'xxx':action =>'xxx' }的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!