Rails 3.2,No Route Matches {:controller =>’xxx’:action =>’xxx“}

我正在尝试使用复选框forms编辑多个记录, 如此Railscast中所示。 从那以后Rails发生了变化,我在路由和表单方面遇到了麻烦。

我有一个PeopleController,我正在尝试制作一个可以同时编辑所有电话号码的表单。 是的,我知道这不是一个有用的function,因为通常没有理由将每个人的电话号码改为同一个东西,但我只是用它来熟悉代码 – 而且它不起作用!

这是我得到的:

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:

 

Listing people


edit_multiple.html.erb:

  update_multiple_path, :html => { :method => :put } do |f| %> 


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 

编辑:

我的路线搞砸了。 他们现在正在如上所示正常工作。

你在混淆单数和复数资源。 edit_multiple_path路由当前配置为单个资源,这意味着它期望将单个Person作为参数传递。 但是,根据定义,您尝试一次更新多个人 – 在这种情况下使用单一路由没有意义。

相反,尝试收集路线:

 # config/routes.rb resources :people collection do post 'update_multiple/:people_ids', :action => 'update_multiple' end end 

这将使以下路线可用:

  update_multiple_invites POST /invites/update_multiple/:people_ids(.:format) invites#update_multiple 

编辑:

在索引视图中,您需要输出表单才能显示它们:

 # 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| %> 

请注意, <% %>解释其中包含的所有内容,但不会将其输出。 您需要使用<%= %>才能实际输出表单的内容。

另外,不要忘记你的部分需要扩展名.erb才能解释Ruby代码。 也许你已经相应地命名了它,但是你的文章描述了没有 .erb的名称.erb

submit请查看代码检查器并validation您是否已路由到GET请求。

如果你得到一个404导致GET请求而不是PUT请求,那是因为:method => :put选项依赖于JavaScript。 您需要确保jquery-rails已正确集成到您的应用中。

你正试图得到这条路线:

没有路由匹配{:controller =>“people”,:action =>“edit_multiple”}

但是在route.rb中你包括:需要id属性

人/:ID / edit_multiple

因此,在表单中,您需要包含人员ID

尝试使用以下代码来获取此路线{:controller =>“people”,:action =>“edit_multiple”}

 resources :people do collection do get :edit_multiple put :update_multiple end end