2016-07-12 5 views
0

현재 사용자가 하나의 제출 버튼으로 동일한 개체의 여러 양식을 제출할 수있는 새로운 기능을 추가하고 있습니다.여러 양식을 레일에 제출하십시오.

나는 'add_inquery'버튼을 추가했는데, 사용자가 클릭 할 때 새로운 양식이 (같은 페이지에서) 렌더링된다. 사용자가 제출할 양식 수를 제한하지 않습니다. 일반적으로 원할 경우 10 개에서 20 개의 양식으로 제출할 수 있습니다.

내가 오류는 다음과 같습니다

정의되지 않은 메서드`fields_for '무기 호에 대한 : NilClass

가 여기 내 코드의 모든 위치 :

_form.html.haml에게 :

.col-md-3 
    = link_to "Add Inquery", pricing_histories_add_inquery_path, remote: true, class: 'btn btn-default', data: {disable_with: "Please wait..."} 

= form_tag pricing_histories_path , html: {class: 'form-horizontal'} do |f| 
    %div.alert.alert-danger.display-hide 
    %button.close{'data-close' => 'alert'} 
    You have some form errors. Please check below. 
    %div.alert.alert-success.display-hide 
    %button.close{'data-close' => 'alert'} 
    Your form validation is successful! 
    %div.row 
    %div.col-xs-12.form-body 
     = f.fields_for '' do |fd| 
     = render 'form_body', f: fd, object: f.object 
    %div.form-actions 
    %div.row 
     %div.col-md-offset-9.col-md-3 
     %button.btn.green{:type => 'submit'} Submit 

_form_body.html.haml :

%div.form-group.form-md-line-input 
    %label.control-label.col-md-3 
    = I18n.t('columns.customer_name') 
    %span.required * 
    %div.col-md-6 
    = f.text_field_tag :customer_name, placeholder: 'Customer Name', class: 'form-control' 
    %div.form-control-focus 
    %span.help-block Enter Customer Name 
%div.form-group.form-md-line-input 
    %label.control-label.col-md-3 
    = I18n.t('columns.actual_customer') 
    %span.required * 
    %div.col-md-6 
    = f.text_field_tag :actual_customer, placeholder: 'Actual Customer', class: 'form-control actual_customer' 
    %div.form-control-focus 
    %span.help-block Enter Actual Customer 
%div.form-group.form-md-line-input 
    %label.control-label.col-md-3 
    = I18n.t('columns.bco') 
    %span.required * 
    .col-md-6 
    .mt-radio-inline 
     %label.mt-radio 
     = f.radio_button :bco, true, :checked => true, class: 'bco' 
     = I18n.t('columns.bco_splited') 
     %span 
     %label.mt-radio 
     = f.radio_button :bco, false, class: 'non_bco' 
     = I18n.t('columns.non_bco_splited') 
     %span 
%div.form-group.form-md-line-input 
    = f.label :trade_id, class: 'col-md-3 control-label' do 
    = I18n.t('columns.trade') 
    %span.required * 
    %div.col-md-6 
    = f.select :trade_id, options_for_select(Trade.all.map{|l| [l.name + '(' + l.code + ')', l.id]}, object.trade_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Trade 
%div.form-group.form-md-line-input 
    = f.label :trade_id, class: 'col-md-3 control-label' do 
    = I18n.t('columns.sub_trade') 
    %span.required * 
    %div.col-md-6 
    = f.select :sub_trade_id, options_for_select(SubTrade.all.map{|l| [l.name + '(' + l.code + ')', l.id]}, object.trade_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Sub Trade 
%div.form-group.form-md-line-input 
    = f.label :bound, class: 'col-md-3 control-label' do 
    = I18n.t('columns.bound') 
    %span.required * 
    .col-md-6 
    = f.select :bound, PricingHistory.get_bound_array, 
     {}, {class: 'form-control select2_category'} 
    %div.form-control-focus 
    %span.help-block Enter Bound 
%div.form-group.form-md-line-input 
    = f.label :sales_week_id, class: 'col-md-3 control-label' do 
    Sales Week 
    %span.required * 
    %div.col-md-6 
    = f.select :sales_week_id, options_for_select(SalesWeek.all.map{|sw| [sw.display, sw.id]}, object.sales_week_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Sales Week 
%div.form-group.form-md-line-input 
    = f.label :origin_location_id, class: 'col-md-3 control-label' do 
    Origin Location 
    %span.required * 
    %div.col-md-6 
    = f.select :origin_location_id, options_for_select(Location.all.map{|l| [l.name + '(' + l.code + ')', l.id]}, object.origin_location_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Origin Location 
%div.form-group.form-md-line-input 
    = f.label :destination_location_id, class: 'col-md-3 control-label' do 
    Destination Location 
    %span.required * 
    %div.col-md-6 
    = f.select :destination_location_id, options_for_select(Location.all.map{|l| [l.name + '(' + l.code + ')', l.id]}, object.destination_location_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Destination Location 
%div.form-group.form-md-line-input 
    = f.label :surcharge_group_id, class: 'col-md-3 control-label' do 
    Surcharge Group 
    %span.required * 
    %div.col-md-6 
    = f.select :surcharge_group_id, options_for_select(SurchargeGroup.all.map{|s| [s.code, s.id]}, object.surcharge_group_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Surcharge Group Code 
%div.form-group.form-md-line-input 
    = f.label :commodity_group_id, class: 'col-md-3 control-label' do 
    Commodity Group 
    %span.required * 
    %div.col-md-6 
    = f.select :commodity_group_id, options_for_select(CommodityGroup.all.map{|s| [s.name + '(' + s.code + ')', s.id]}, object.surcharge_group_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Commodity Group 
%div.form-group.form-md-line-input 
    = f.label :tier_id, class: 'col-md-3 control-label' do 
    Tier 
    %span.required * 
    %div.col-md-6 
    = f.select :tier_id, options_for_select(Tier.all.map{|s| [s.tier, s.id]}, object.tier_id), {}, {class: 'form-control'} 
    %div.form-control-focus 
    %span.help-block Enter Tier 
%div.form-group.form-md-line-input 
    = f.label :d2_volume, class: 'col-md-3 control-label' do 
    D2 Volume 
    %span.required * 
    %div.col-md-6 
    = f.number_field :d2_volume, class: 'form-control', 'aria-required' => true, 'aria-describedby' => 'number-error' 
    %div.form-control-focus 
    %span.help-block Enter D2 Volume 
%div.form-group.form-md-line-input 
    = f.label :d4_volume, class: 'col-md-3 control-label' do 
    D4 Volume 
    %span.required * 
    %div.col-md-6 
    = f.number_field :d4_volume, class: 'form-control', 'aria-required' => true, 'aria-describedby' => 'number-error' 
    %div.form-control-focus 
    %span.help-block Enter D4 Volume 
%div.form-group.form-md-line-input 
    = f.label :d5_volume, class: 'col-md-3 control-label' do 
    D5 Volume 
    %span.required * 
    %div.col-md-6 
    = f.number_field :d5_volume, class: 'form-control', 'aria-required' => true, 'aria-describedby' => 'number-error' 
    %div.form-control-focus 
    %span.help-block Enter D5 Volume 
%div.form-group.form-md-line-input 
    = f.label :d7_volume, class: 'col-md-3 control-label' do 
    D7 Volume 
    %span.required * 
    %div.col-md-6 
    = f.number_field :d7_volume, class: 'form-control', 'aria-required' => true, 'aria-describedby' => 'number-error' 
    %div.form-control-focus 
    %span.help-block Enter D7 Volume 
%div.form-group.form-md-line-input 
    = f.label :r2_volume, class: 'col-md-3 control-label' do 
    R2 Volume 
    %span.required * 
    %div.col-md-6 
    = f.number_field :r2_volume, class: 'form-control', 'aria-required' => true, 'aria-describedby' => 'number-error' 
    %div.form-control-focus 
    %span.help-block Enter R2 Volume 
%div.form-group.form-md-line-input 
    = f.label :r5_volume, class: 'col-md-3 control-label' do 
    R5 Volume 
    %span.required * 
    %div.col-md-6 
    = f.text_field_tag :r5_volume, class: 'form-control', 'aria-required' => true, 'aria-describedby' => 'number-error' 
    %div.form-control-focus 
    %span.help-block Enter R5 Volume 

pricing_history_controller.rb :

class PricingHistoriesController < ApplicationController 
    before_action :set_pricing_history, only: [:show, :edit, :update, :destroy] 

    def add_inquery 

    end 

    def index 
    @pricing_histories = current_user.pricing_histories.all.order(updated_at: :desc) 
    end 

    def show 
    end 

    def new 
    @pricing_history = current_user.pricing_histories.build 
    @pricing_histories = [] 
    6.times do 
     @pricing_histories << @pricing_history 
    end 
    end 

    def edit 
    end 

    def create 
    params["pricing_history"].each do |pricing_history| 
     pricing_history = current_user.pricing_histories.build(pricing_history_params(pricing_history)) 
     pricing_history.tier_id = 3 
     customer_name = pricing_history.customer_name 

     if pricing_history.bco == false && Scra.exists?(actual_customer: pricing_history.actual_customer) 
     return redirect_to new_pricing_history_path, notice: init_message(:error, t('scras.record_exist')) 
     end 

     if (scra = Scra.find_by_customer_name(customer_name)).present? 
     pricing_history.tier_id = Tier.find_by_scra_id(scra.id).id 
     end 

     if pricing_history.save 
     # pricing_history.calculate! 
     # redirect_to pricing_histories_path, notice: init_message(:success, t('message.new_success', page_name: t('page_name.pricing_history'))) 
     else 
     render :new 
     end 
    end 
    # @pricing_history = current_user.pricing_histories.build(pricing_history_params) 
    # @pricing_history.tier_id = 3 
    # customer_name = @pricing_history.customer_name 
    # 
    # if @pricing_history.bco == false && Scra.exists?(actual_customer: @pricing_history.actual_customer) 
    # return redirect_to new_pricing_history_path, notice: init_message(:error, t('scras.record_exist')) 
    # end 
    # 
    # if (scra = Scra.find_by_customer_name(customer_name)).present? 
    # @pricing_history.tier_id = Tier.find_by_scra_id(scra.id).id 
    # end 
    # 
    # if @pricing_history.save 
    # @pricing_history.calculate! 
    # redirect_to @pricing_history, notice: init_message(:success, t('message.new_success', page_name: t('page_name.pricing_history'))) 
    # else 
    # render :new 
    # end 
    end 

    def update 
    if @pricing_history.update(pricing_history_params) 
     if Scra.exists?(customer_name: @pricing_history.customer_name) 
     @pricing_history.update_columns(tier_id: 3) 
     end 
     redirect_to @pricing_history, notice: init_message(:success, t('message.update_success', page_name: t('page_name.price_history'))) 
    else 
     render :edit 
    end 
    end 

    def destroy 
    @pricing_history.destroy 
    redirect_to pricing_histories_url, notice: init_message(:success, t('message.delete_success', page_name: t('page_name.price_history'))) 
    end 

    private 
    def set_pricing_history 
     @pricing_history = PricingHistory.find(params[:id]) 
    end 

    def pricing_history_params(element_params) 
     element_params.permit(
     :sales_week_id, 
     :trade_id, 
     :sub_trade_id, 
     :bound, 
     :origin_location_id, 
     :destination_location_id, 
     :surcharge_group_id, 
     :commodity_group_id, 
     :tier_id, 
     :customer_name, 
     :actual_customer, 
     :bco, 
     :d2_volume, 
     :d4_volume, 
     :d5_volume, 
     :d7_volume, 
     :r2_volume, 
     :r5_volume 
    ) 
    end 
end 

답변

0

당신은 form_tag 정의 된 형태의 내부 f.fields_for을 요구하고있다. f.fields_for을 사용하려면 form_for을 사용하여 정의 된 모델이 있어야합니다. 당신이 form_for를 사용하지 않고 fields_for를 사용하려면

, 처음에 f.을 제거하려고 그냥 사용 :이 자습서를 따라하고

fields_for '' do |fd| 
+0

고맙습니다. 사실, 저는 여러분의 해결책을 여기에서 보았 기 전에 그것을 고쳤습니다. 그리고 이제 또 다른 문제가 생깁니다. 다른 질문으로 게시 할 것입니다. 좋은 하루 되세요. :)) –

0

fields_for는 특정 모델 객체의 범위를 만들고 form_for 도우미에 대해 사용할 수 있습니다.

난 당신이 form_tag을 사용하고 있고이 때 form_tag에 대한 fields_for 방법을 기대하고 있다고 보았다. 따라서 오류가 발생합니다. fields_for

업데이트에 대한 더 많은 정보를 원하시면 확인하시기 바랍니다 : 아래로

는 fields_for를 사용합니다. 컨트롤러에서 new 작업으로 시작된 pricing_histories 개체를 반복해야합니다.

= form_tag pricing_histories_path, **** do || 
    - @pricing_histories.each do |pricing_history| 
    = fields_for 'pricing_history[]', pricing_history do |p| 
     = render 'form_body', f: pricing_history, object: @pricing_history 
+0

, 그는 때 form_tag와 fields_for 사용했다. 그러나 form_for로 다시 변경하면 또 다른 오류가 발생합니다. 어떤 제안? –

+0

@ ĐỗTiến - 시나리오에서'fields_for' 헬퍼 메소드를 사용하기위한 나의 대답이 업데이트되었습니다. 일단 그런 식으로 시도하고 어떤 문제인지 알려주세요. –

+0

사실 나는 form_for로 돌아 가기로 결정했습니다. 그러나 어쨌든 당신의 도움에 감사드립니다. 좋은 하루 되세요. –

관련 문제