在我的应用程序中,我有模型“regulations”,用于创建法规的表单如下所示:
- if %W(edit update).include? action_name
= content_for :heading, t('partners.regulations.edit')
- else
= content_for :heading, t('partners.regulations.new')
.row
.col-md-6
= horizontal_simple_form_for [:partners, @regulation] do |f|
= f.error_notification
%ul.nav.nav-pills
%li
= link_to image_tag("flag_pl.png"), '#PL', class: 'regulation_pl'
%li
= link_to image_tag("flag_en.png"), '#EN', class: 'regulation_en'
.polish_regulation
%h1.page-header
= t('partners.regulations.polish')
= f.input :content_pl, :as => :ckeditor, :input_html => { :ckeditor => {:toolbar => 'Pure', width: 750} }, error: false
.english_regulation
%h1.page-header
= t('partners.regulations.english')
= f.input :content_en, :as => :ckeditor, :input_html => { :ckeditor => {:toolbar => 'Basic', width: 750} }, error: false
- if %W(edit update).include? action_name
= f.submit t('actions.save_changes'), class: "btn btn-lg btn-default"
- else
= f.submit t('partners.regulations.save'), class: "btn btn-lg btn-default"在我的路线上:
namespace :partners do
resources :regulations, as: :rental_company_regulation, except: :show
end我的控制器看起来是这样的:
module Partners
class RegulationsController < ApplicationController
include Partners::BaseController
load_and_authorize_resource through: :rental_company, singleton: true
before_action :set_breadcrumbs
before_action :set_regulation, only: :new
def new
if @rental_company_regulation
redirect_to edit_partners_rental_company_regulation_path(@rental_company_regulation)
end
add_breadcrumb t('partners.regulations.new')
end
def create
if @regulation.save
flash[:notice] = t('partners.regulations.created')
redirect_to partners_root_path
else
add_breadcrumb t('partners.regulations.new')
render :new
end
end
def edit
add_breadcrumb t('partners.regulations.edit')
end
def update
if @regulation.update(regulation_params)
flash[:notice] = t('partners.regulations.updated')
redirect_to partners_root_path
else
add_breadcrumb t('partners.regulations.edit')
render :edit
end
end
protected
def set_regulation
@rental_company_regulation = Regulation.where(rental_company_id: rental_company).first
end
def set_breadcrumbs
add_breadcrumb current_partner.rental_company.name, :partners_root_path
end
private
def regulation_params
params.require(:regulation).permit(:content_pl, :content_en, :rental_company_id)
end
end
end 从零开始创建资源可以正常工作。但是我转到:"http://localhost:3000/partners/regulations/4/edit"来编辑规则,我有以下错误:
undefined method `partners_regulation_path' for #<#<Class:0x0000000242fae8>:0x00000007d9afb0>我解决不了这个问题。怎么了?
编辑:
耙路:
partners_rental_company_regulation_index GET /partners/regulations(.:format) partners/regulations#index
POST /partners/regulations(.:format) partners/regulations#create
new_partners_rental_company_regulation GET /partners/regulations/new(.:format) partners/regulations#new
edit_partners_rental_company_regulation GET /partners/regulations/:id/edit(.:format) partners/regulations#edit
partners_rental_company_regulation PATCH /partners/regulations/:id(.:format) partners/regulations#updateEdit2:我也改变了我的格式,如下所示:
= horizontal_simple_form_for [:partners, @regulation], url: partners_rental_company_regulation_path现在,我可以编辑现有资源,但不能创建一个新的资源。当我试图创建新的一个时,我有以下错误:
No route matches {:action=>"update", :controller=>"partners/regulations"} missing required keys: [:id]怎么了?
发布于 2014-06-11 06:23:06
as
为了简化user2675613的回答,您的错误基本上是由在您的路由中使用 argument造成的。
as:基本上允许您“命名”路线:
#config/routes.rb
resources :users # -> users_path
resources :users, as: :members # -> members_path误差
您的错误如下:
undefined method `partners_regulation_path'这基本上意味着你在使用一条不存在的路径。这是因为您在路由中使用了as:选项:
#config/routes.rb
resources :partners do
resources :regulations #-> partners_regulation_path
end
resources :partners do
resources :regulations, as: "test" # -> partners_test_path
end你的路线证实了这一点:
partners_rental_company_regulation_index
new_partners_rental_company_regulation
edit_partners_rental_company_regulation
partners_rental_company_regulationFix
解决此问题的方法是从路由中删除as:选项:
#config/routes.rb
resources :partners do
resources :regulations #-> partner_regulations_path
end或者,如果您想为您的路径保留自定义name,则必须更改对parental_company_regulation_path的路径引用
发布于 2014-06-11 05:50:24
正如您在rake中看到的那样,您的url编辑规则是edit_partners_rental_company_regulation GET /partners/regulations/:id/edit(.:format),创建一个新的规则,url是new_partners_rental_company_regulation GET /partners/regulations/new(.:format)。
您需要改变您在这里使用表单的方式。您可以为表单的所有公共字段创建一个分部,但您需要将表单中的url部分分隔开来,以发出创建和更新请求。
您可以使用以下内容制作部分_form.html.haml:
= f.error_notification
%ul.nav.nav-pills
%li
= link_to image_tag("flag_pl.png"), '#PL', class: 'regulation_pl'
%li
= link_to image_tag("flag_en.png"), '#EN', class: 'regulation_en'
.polish_regulation
%h1.page-header
= t('partners.regulations.polish')
= f.input :content_pl, :as => :ckeditor, :input_html => { :ckeditor => {:toolbar => 'Pure', width: 750} }, error: false
.english_regulation
%h1.page-header
= t('partners.regulations.english')
= f.input :content_en, :as => :ckeditor, :input_html => { :ckeditor => {:toolbar => 'Basic', width: 750} }, error: false然后,在您的edit.html.haml中您可以拥有:
= content_for :heading, t('partners.regulations.edit')
.row
.col-md-6
= horizontal_simple_form_for [:partners, @regulation], url: partners_rental_company_regulation_path do |f|
= render :partial => "form", :locals => { :f => f } #you need to pass other required locals too else it'll give error
= f.submit t('actions.save_changes'), class: "btn btn-lg btn-default"类似地,在您的new.html.haml中您可以拥有:
= content_for :heading, t('partners.regulations.new')
.row
.col-md-6
= horizontal_simple_form_for [:partners, @regulation], url: partners_rental_company_regulation_index_path do |f|
= render :partial => "form", :locals => { :f => f } #you need to pass other required locals too else it'll give error
= f.submit t('partners.regulations.save'), class: "btn btn-lg btn-default"https://stackoverflow.com/questions/24154965
复制相似问题