簡體   English   中英

Rails 4:通過has_many關聯創建新記錄

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

我正嘗試通過“客戶”為“廣告系列”創建新記錄,如下所示。 表單提交正確,但是沒有為客戶創建新的活動記錄。 任何建議將不勝感激。

楷模

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

如果此表單僅添加一個廣告系列,我想您應該將這個exect廣告系列手動添加到客戶。 如果以這種方式更新所有廣告系列模型,則應將所有廣告系列排列在一起。 因此,這意味着需要執行另一個控制器操作。

另一種方法:檢查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

檢查一下,不確定是否可行。

也許它會起作用,如何在注釋中提出建議:在customer_params方法中更改campaigns_attributes => campaign_attributes ,並f.fields_for :campaigns形式為f.fields_for :campaigns (屬於@Nikita Chernov)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM