首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Rails 4强参数未知参数误差

Rails 4强参数未知参数误差
EN

Stack Overflow用户
提问于 2015-01-12 00:48:44
回答 1查看 367关注 0票数 0

简单的一对多的关系,并在服务器日志中获取一个不允许的参数。

杯1-*>接触

帕拉姆看上去像

代码语言:javascript
运行
复制
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Ay/wRwJTr4u5Vgu5oOrk7z4RC/OfeLJdN9WXuoyU7iQ=", "cup"=>{"name"=>"cupname", "location"=>"Somewhe", "type_ids"=>"1", "contact"=>{"first_name"=>"Greg", "last_name"=>"Ander", "email"=>"email@me.com", "telephone"=>"444906398"}}, "commit"=>"Create new cup"}

在cups_controller.rb中

代码语言:javascript
运行
复制
class CupsController < ApplicationController

def create
    @cup = Cup.new(cup_params)
    render nothing: true
end

private
        def cup_params
            params.require(:cup).permit(:name,:location,:type_ids,   
                :contacts_attributes => [:first_name, :last_name, :email, :telephone ])
        end

在杯/new.html.erb-f是杯的主要形式。

代码语言:javascript
运行
复制
<%= f.fields_for :contacts do |contact_info| %>    
      <%= contact_info.label :first_name %><%= contact_info.text_field :first_name %>
      <%= contact_info.label :last_name %><%= contact_info.text_field :last_name %>
      <%= contact_info.label :email %><%= contact_info.email_field :email %>
      <%= contact_info.label :telephone %><%= contact_info.telephone_field :telephone %>
      <% end %>

继续找Unpermitted parameter contact

杯型有accepts_nested_attributes_for :contacts

模型文件

代码语言:javascript
运行
复制
class Cup < ActiveRecord::Base
    has_many :contacts
    has_and_belongs_to_many :types
    accepts_nested_attributes_for :contacts
end

class Contact < ActiveRecord::Base
    belongs_to :cup
end

编辑

添加了更新的cup_params和控制器类。也是模特。

EN

回答 1

Stack Overflow用户

发布于 2015-01-12 00:58:10

您需要引用contacts_attributes,而不是联系自己。

代码语言:javascript
运行
复制
def cup_params
  params.require(:cup).permit(:name,:location,:type_ids,   
    contacts_attributes: [:first_name, :last_name, :email, :telephone ])
end
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27893850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档