在我的应用程序中,我有模型"Regulation",用于创建法规的表单看起来像这样:
- 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#update
Edit2:我也改变了我的形式,看起来像这样:
= horizontal_simple_form_for [:partners, @regulation], url: partners_rental_company_regulation_path
现在我可以编辑现有的资源,但不能创建一个新的。当我尝试创建一个新的,我有以下错误:
No route matches {:action=>"update", :controller=>"partners/regulations"} missing required keys: [:id]
怎么了?
为了简化user2675613
的答案,你的错误基本上是由在你的路由中使用as
参数引起的
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_regulation
修复
解决这个问题的方法是从你的路由中删除as:
选项:
#config/routes.rb
resources :partners do
resources :regulations #-> partner_regulations_path
end
或者,如果您想为路径保留自定义的name
,则必须将路径引用更改为parental_company_regulation_path
你可以在你的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"