如何在多控制器视图中修复 form_for 联系人所需的参数
How to fix params require for form_for contact in multi controller view
我的联系人有问题form_for,
我有一个联系表,我想在我的产品视图中呈现我的联系表。
如果我这样做,我的 require params
有问题
我的 contact_params 在我的 ContactController 中工作:
def contact_params
params.require(:contact).permit(:name, :email, :body)
end
我已经添加到我的 ApplicationController
def set_contact
@contact = Contact.new params[:contact] || {}
end
并且在我的 ProductsControler 中
show
@contact = :set_contact
end
我的路线是:
get 'contact', to: 'contacts#new', as: 'new_contact'
post 'contact', to: 'contacts#create', as: 'create_contact'
我的渲染是一样的:
<%= render :partial => "contacts/contact" %>
以及我在 _contact.html.erb 中的观点:
<%= form_for @contact, url: create_contact_url do |f| %>
<%= f.text_field :name %>
<%= f.email_field :email %>
<%= f.text_area :body %>
<%= f.submit 'Send' %>
Rails 说我:
ActionController::ParameterMissing (param is missing or the value is empty: contact):
如果我删除了不起作用的 require(:contact) rails 说我:
Unpermitted parameter: :contact
谢谢
尝试将参数与产品参数合并
参数[:product].merge!(:contact)
例如
定义 company_params
params[:company].merge!(users_attributes: {'0': user_params.to_h })
params.require(:公司).permit(
:姓名, :contact_person_name, :电子邮件,
:contact_person_number, :terms_and_conditions )
结束
听说 users_attributes 来自我的其他表格。
适合我。
祝你好运。
您在 ProductsControler @contact = :set_contact
的方法 #show 中有拼写错误,您应该在没有 :
的情况下调用方法 set_contact
正确的代码:@contact = set_contact
我的联系人有问题form_for, 我有一个联系表,我想在我的产品视图中呈现我的联系表。 如果我这样做,我的 require params
有问题我的 contact_params 在我的 ContactController 中工作:
def contact_params
params.require(:contact).permit(:name, :email, :body)
end
我已经添加到我的 ApplicationController
def set_contact
@contact = Contact.new params[:contact] || {}
end
并且在我的 ProductsControler 中
show
@contact = :set_contact
end
我的路线是:
get 'contact', to: 'contacts#new', as: 'new_contact'
post 'contact', to: 'contacts#create', as: 'create_contact'
我的渲染是一样的:
<%= render :partial => "contacts/contact" %>
以及我在 _contact.html.erb 中的观点:
<%= form_for @contact, url: create_contact_url do |f| %>
<%= f.text_field :name %>
<%= f.email_field :email %>
<%= f.text_area :body %>
<%= f.submit 'Send' %>
Rails 说我:
ActionController::ParameterMissing (param is missing or the value is empty: contact):
如果我删除了不起作用的 require(:contact) rails 说我:
Unpermitted parameter: :contact
谢谢
尝试将参数与产品参数合并 参数[:product].merge!(:contact)
例如 定义 company_params params[:company].merge!(users_attributes: {'0': user_params.to_h }) params.require(:公司).permit( :姓名, :contact_person_name, :电子邮件, :contact_person_number, :terms_and_conditions ) 结束
听说 users_attributes 来自我的其他表格。 适合我。
祝你好运。
您在 ProductsControler @contact = :set_contact
的方法 #show 中有拼写错误,您应该在没有 :
set_contact
正确的代码:@contact = set_contact