简体   繁体   English

Rails 4:通过has_many关联创建新记录

[英]Rails 4: Create new records through has_many association

I am trying to create a new record for 'campaign' via the 'customer' as follows. 我正尝试通过“客户”为“广告系列”创建新记录,如下所示。 The form submits correctly, however no new campaign record is created for the customer. 表单提交正确,但是没有为客户创建新的活动记录。 Any advise would be greatly appreciated. 任何建议将不胜感激。

Models 楷模

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

Customer Controller - Update Method only shown 客户控制器-仅显示更新方法

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

And the form I am using 我正在使用的表格

<%= 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 %>

And the console output (with error, is as follows) 和控制台输出(有错误,如下)

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

If this form performs adding only one campaign i guess you should handly add this exect campaign to customer. 如果此表单仅添加一个广告系列,我想您应该将这个exect广告系列手动添加到客户。 If you update all campaign model in such way, you should have all your campaigns in array. 如果以这种方式更新所有广告系列模型,则应将所有广告系列排列在一起。 So it implies another controller action for this. 因此,这意味着需要执行另一个控制器操作。

Another way: to check if params contains field campain and then just add it to customer object: 另一种方法:检查params是否包含field 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

Check this out, not sure it works. 检查一下,不确定是否可行。

Or maybe it would work how to suggests in comment: change campaigns_attributes => campaign_attributes in customer_params method, and f.fields_for :campaigns in form (belongs to @Nikita Chernov) 也许它会起作用,如何在注释中提出建议:在customer_params方法中更改campaigns_attributes => campaign_attributes ,并f.fields_for :campaigns形式为f.fields_for :campaigns (属于@Nikita Chernov)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Rails 3.2中的关联在has_many中创建记录 - How to create records in has_many through association in rails 3.2 在Rails 4中通过创建新对象来创建has_many:through关联 - Create has_many :through association in Rails 4 with building a new object Rails-has_many:通过查找没有关联的记录 - Rails - has_many :through find records which have no association 为rails 3 has_many创建的错误数据库记录:通过关联 - incorrect database records created for rails 3 has_many :through association 将关联记录的顺序保存在Rails has_many中:通过关联 - Saving the order of associated records in a Rails has_many :through association 如何将记录添加到has_many:通过rails中的关联 - how to add records to has_many :through association in rails Rails:通过关联显示has_many上的所有关联记录 - Rails: Show all associated records on a has_many through association 如何将记录添加到has_many:通过Ruby on Rails中的关联 - how to add records to has_many :through association in Ruby on rails Rails:将属性值添加到has_many:through关联的联接表中的几个新记录中 - Rails: Add attribute values to several new records in a join table in a has_many :through association Rails:has_many通过关联,以及用于创建新实例的表单 - Rails: has_many through association, and the form for creating new instances
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM