Ruby on rails 3 Rails 3-表单“U用于导致路径/方法错误的多重化”;新";行动,但不在“上”;编辑";

Ruby on rails 3 Rails 3-表单“U用于导致路径/方法错误的多重化”;新";行动,但不在“上”;编辑";,ruby-on-rails-3,routes,form-for,pluralize,Ruby On Rails 3,Routes,Form For,Pluralize,当我在应用程序中转到路径:/genre/new时,出现以下错误: myapp/app/views/genre/_form.html.erb where line #1 raised: undefined method `genres_path' for #<#<Class:0x007fdcb39edcb0>:0x007fdcb39e8080> 在genre_controller.rb中,我的“新建”和“编辑”操作如下: def new @genre = Ge

当我在应用程序中转到路径:
/genre/new
时,出现以下错误:

myapp/app/views/genre/_form.html.erb where line #1 raised:
undefined method `genres_path' for #<#<Class:0x007fdcb39edcb0>:0x007fdcb39e8080>
在genre_controller.rb中,我的“新建”和“编辑”操作如下:

  def new
    @genre = Genre.new
    current_user.authorize! :create, @genre  # cancan authorization

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @genre }
    end
  end

  def edit
    @genre = Genre.find(params[:id])
    current_user.authorize! :update, @genre   # cancan authorization
  end
我在我的代码库中搜索了字符串“genres”,它出现的唯一地方就是日志,所以我确信这不是我代码中的错误

我的猜测是Rails路由系统正确地将“流派”多重化为“流派”,但form_for(或依赖项)正在创建多重化的“流派”,但仅当传递给它的参数为空或“new”时

鉴于错误发生在“genres_path”附近,我在routes.rb文件中尝试了以下各种组合,但它们没有解决问题:

  match "/genres" => "genre#index", :as => :genre
  match "/genres/:id(.:format)" => "genre#show", :as => :genre
  match "/genre" => "genre#index", :as => :genres
  match "/genre/:id(.:format)" => "genre#show", :as => :genres
有没有想过我该如何解决这个问题

编辑:以下是由我的
routes.rb
文件中的
resources:genre
语句生成的路由:

genre_index GET    /genre(.:format)           {:action=>"index", :controller=>"genre"}
            POST   /genre(.:format)           {:action=>"create", :controller=>"genre"}
  new_genre GET    /genre/new(.:format)       {:action=>"new", :controller=>"genre"}
 edit_genre GET    /genre/:id/edit(.:format)  {:action=>"edit", :controller=>"genre"}
      genre GET    /genre/:id(.:format)       {:action=>"show", :controller=>"genre"}
            PUT    /genre/:id(.:format)       {:action=>"update", :controller=>"genre"}
            DELETE /genre/:id(.:format)       {:action=>"destroy", :controller=>"genre"}
在new.html.erb上尝试

<%= form_for(@genre, :url => genre_path, :method => :post) do |f| %>
试试这个

<%= form_for(@genre, :url => {:action=>"create", :controller=>"genre"}, :method => :post) do |f| %>
{:action=>“create”,:controller=>“genre”},:method=>:post)do | f |%>

谢谢你的建议。我尝试了它,但它导致了一个不同的错误:没有路由匹配{:action=>“show”,:controller=>“genre”}。但这是一个虚假的错误-我有一个这样的路线。当我运行“rake routes”时,我得到:genre-get/genre/:id(:format){:action=>“show”,:controller=>“genre”}错误消息说的是我丢失的确切路由。你知道是什么原因造成的吗?你能为所有类型的路由添加rake路由的输出吗?另请参阅更新的答案-将方法指定为put或post请参阅问题中我的路由,在底部。尝试了:put和:post as:methods-与之前相同的错误(“没有路由匹配…”),也尝试了我在文档中看到的一些东西,但没有成功。这真让我难堪。谢谢你的帮助。我尝试了你最后一组关于form_for的论点,但没有成功。查看多部词典,我发现“体裁”是一种可以接受的多元化。所以我决定不在这个问题上与Rails抗争。我返回并更改了控制器、助手和db表的名称,以及一些链接到语句的名称。现在一切都好了。我猜这个故事的寓意是:永远不要在多元化的轨道上抗争——你最终会失败的。再次感谢你的帮助。我有一个类似的问题,与复数无关。我使用了一个带有名称空间(Admin)控制器的非名称空间模型类(Book),因此基于资源的路由被声明为“namespace:Admin do resources:books end”。名为route的索引的正确路径是“admin\u books\u path”。但是,正如在这个答案中提供的链接中所解释的,form_for(@book)将把路径改为“books_path”,因此我必须重写:url,写“form_for(@book,:url=>admin_books_path)”,才能让事情正常进行。
POST   /genre(.:format)           {:action=>"create", :controller=>"genre"}
<%= form_for(@genre, :url => {:action=>"create", :controller=>"genre"}, :method => :post) do |f| %>