【问题标题】:Rails 4: Create new records through has_many associationRails 4:通过 has_many 关联创建新记录
【发布时间】:2014-05-26 13:06:48
【问题描述】:

我正在尝试通过“客户”为“广告系列”创建一条新记录,如下所示。表单正确提交,但没有为客户创建新的活动记录。任何建议将不胜感激。

模型

class Customer::Customer < User
  has_one :library, dependent: :destroy
  has_many :campaigns, dependent: :destroy
  accepts_nested_attributes_for :campaigns, :library,  allow_destroy: true
end

class Customer::Campaign < ActiveRecord::Base
  belongs_to :customer
end

客户控制器 - 仅显示更新方法

class Customer::CustomersController < ApplicationController
  layout "customer_layout"
  def update
    session[:return_to] ||= request.referer
    @customer = User.find(params[:id])
    if @customer.update_attributes(customer_params)
      flash[:success] = "Profile updated"
      redirect_to @customer
    else
      flash[:notice] = "Invalid"
      redirect_to session.delete(:return_to)
    end
  end

  def customer_params
    params.require('customer_customer').permit(:name, :email, :password, :password_confirmation, :country_code, :state_code, :postcode, :first_name, :surname, campaigns_attributes:[:name, :description, :start_date, :complete_date ])
  end
end

还有我正在使用的表格

<%= form_for @customer do |f| %>
  <%= render 'shared/customer_error_messages' %>
  <%= f.fields_for :campaign do |builder| %>
    <%= builder.label :name, "Campaign Name" %></br>
    <%= builder.text_field :name %>
  <% end %>
  <div class="actions">
    <%= f.submit class: "btn btn-large btn-primary"%>
  </div>
<% end %>

以及控制台输出(有错误,如下)

Started PATCH "/customer/customers/38" for 127.0.0.1 at 2014-05-26 23:13:10 +1000
Processing by Customer::CustomersController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"9ChLYna0/VfZSMJOa2yGgvWTT61XkjoYwlVup/Pbbeg=", "customer_customer"=>{"campaign"=>{"name"=>"My new campaign"}}, "commit"=>"Update Customer", "id"=>"38"}
  User Load (0.5ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '0e8b4ba6dc957e76948fc22bae57673404deb9fe' LIMIT 1
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "38"]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1  [["id", "38"]]
Unpermitted parameters: campaign

【问题讨论】:

  • 没有错误?至少显示日志。
  • 日志中出现以下错误:Unpermitted parameters: campaign
  • 您的参数哈希中有一个"campaign" 键,但它应该是"campaigns_attributes",正如您在#customer_params 方法中正确编写的那样。尝试使用复数形式的f.fields_for :campaigns

标签: ruby-on-rails ruby activerecord has-many


【解决方案1】:

如果此表单只添加一个广告系列,我想您应该手动将此 exect 广告系列添加给客户。如果您以这种方式更新所有广告系列模型,您应该将所有广告系列放在阵列中。因此,这意味着另一个控制器操作。

另一种方法:检查 params 是否包含字段 campain,然后将其添加到 customer 对象:

def update
  session[:return_to] ||= request.referer
  @customer = User.find(params[:id])

  if new_campaign_customer_params.has_key? :campaign
    @customer.campaigns << Campaign.new(new_campaign_customer_params)
    if @customer.save
      flash[:success] = "Profile updated"
      redirect_to @customer
    else
      flash[:notice] = "Invalid"
      # you should rerender your view here, set it path
      render 'your_view'
    end
  else
    if @customer.update_attributes(customer_params)
      flash[:success] = "Profile updated"
      redirect_to @customer
    else
      flash[:notice] = "Invalid"
      redirect_to session.delete(:return_to)
    end
  end
end

def new_campaign_customer_params
  params.require('customer_customer').permit(campaign_attributes:[:name, :description, :start_date, :complete_date ])
end

检查一下,不确定它是否有效。

或者它可能会在评论中建议如何工作:在 customer_params 方法中更改 campaign_attributes,在形式中更改 f.fields_for :campaigns(属于 @Nikita Chernov)

【讨论】:

    猜你喜欢
    • 2017-01-24
    • 2020-11-20
    • 2013-01-08
    • 1970-01-01
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2014-12-16
    相关资源
    最近更新 更多