使用simple_form_for

时间:2016-07-26 09:26:31

标签: ruby-on-rails ruby ruby-on-rails-4

使用simple_form_for时遇到路由问题。我知道这与复数有关,但同样的形式对于编辑/更新方法非常有效。

undefined method `companies_path'
Did you mean?  companys_path
               company_path

companys_controller

def new
    @company = Company.new
end

def create
    @company = Company.new(company_params)

    if @company.save
        redirect_to @company
    else
        render 'new'
    end

end

_form.html.haml

= simple_form_for @company do |f|
 = f.input :name
 = f.input :description
 = f.input :website

 = f.button :submit

佣金路线

    companys GET    /companys(.:format)          companys#index
             POST   /companys(.:format)          companys#create
 new_company GET    /companys/new(.:format)      companys#new
edit_company GET    /companys/:id/edit(.:format) companys#edit
     company GET    /companys/:id(.:format)      companys#show
             PATCH  /companys/:id(.:format)      companys#update
             PUT    /companys/:id(.:format)      companys#update
             DELETE /companys/:id(.:format)      companys#destroy

我想问题是,编辑/更新是针对' companys_path' &new / create将用于' companies_path'。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

首先,您应该遵循rails命名惯例,并将控制器从companys_controller.rb重命名为companies_controller.rb。 重命名后,您的问题将得到解决。

Simple form假设您已遵循铁路命名惯例并在companies_path发出routes中不存在的请求。

答案 1 :(得分:1)

控制器名称应为复数,因此在您的情况下,它应为companies_controller

按照以下步骤进行更改

  • 将控制器重命名为companies_controller.rb
  • 将控制器类名更改为

    class CompaniesController < ActionController::Base
      # controller code
    end
    
  • routes.rb

    的变化
    resources :companies
    

修改

Controller naming conventions

Greg

中的

链接