2017-04-26 55 views
0

我有2個表格,landslidessources(也許並不相互關聯)。我想要一個讓用戶填寫信息然後提交給兩個表的表單。這是我目前的形式,而不sources字段:在數據庫中提交一個表格到2個表格 - ruby​​ on rails

= form_for :landslide, :url => {:controller => 'landslides', :action => 'create'} do |f| 

     .form-inputs 
     %form#landslideForm 
      .form-group.row 
      %label.col-sm-2.col-form-label{for: "textinput"}Date 
      .col-sm-10 
       = f.date_select :start_date, :class => "form-control" 
      #Some fields 
    .form-actions 
     = f.button :submit, class: "btn btn-lg btn-primary col-sm-offset-5", id: "submitButton" 

及參數:

def landslide_params 
      params.require(:landslide).permit(:start_date, :continent, :country, :location, :landslide_type, :lat, :lng, :mapped, :trigger, :spatial_area, :fatalities, :injuries, :notes) 
    end 

    def source_params 
     params.require(:source).permit(:url, :text, :landslide_id) 
    end 

也有在sources電話landslide_id這需要從表landslides滑坡ID列。所以當用戶提交一個新的山體滑坡時,我如何才能採取即將到來的山體滑坡ID(這是自動增量,用戶不需要填寫)?

謝謝!

回答

1

HTML問題不允許嵌套<form>元素,你可以不傳遞尚未被持久化的記錄的id(因爲它沒有id)。

要建立在相同的請求嵌套資源使用accepts_nested_attributes_for

class Landslide 
    # or has_many 
    has_one :source 
    accepts_nested_attributes_for :source  
end 

class Source 
    belongs_to :landslide 
end 

這意味着,你可以做Landslide.create(source_attributes: { foo: 'bar' }),它會同時創建LandslideSource記錄,並通過sources.landslide_id會自動將它們鏈接。

要創建表單輸入使用fields_for

# use convention over configuration 
= form_for @landslide do |f| 
    .form-inputs 
    .form-group.row 
     # use the form builder to create labels instead 
     = f.label :start_date, class: 'col-sm-2 col-form-label' 
     .col-sm-10 
     = f.date_select :start_date, class: "form-control" 
    %fieldset 
     %legend Source 
     = f.fields_for :sources do |s| 
     .form-group.row 
      = s.label :url, class: 'col-sm-2 col-form-label' 
      .col-sm-10 
      = s.text_field :url, class: "form-control" 
     # ... 

class LandslidesController 

    # ... 

    def new 
    @landslide = Landslide.new 
    # this is needed to seed the form with inputs for source 
    @landslide.source.new 
    end 

    def create 
    @landslide = Landslide.new(landslide_params) 
    if @landslide.save 
     redirect_to @landslide 
    else 
     @landslide.source.new unless @landslide.source.any? 
     render :new 
    end 
    end 

    private 
    def landslide_params 
    params.require(:landslide).permit(
     :start_date, :continent, :country, 
     :location, :landslide_type, 
     :lat, :lng, :mapped, :trigger, :spatial_area, 
     :fatalities, :injuries, :notes, 
     source_attributes: [ :url, :text ] 
    ) 
    end 
end 
+0

謝謝。這個答案非常詳細。但是我仍然收到錯誤'發現未經許可的參數:來源' –

+0

如果您想調試幫助,請以此爲出發點提出一個新問題。 – max

+0

我認爲這是關於來源/來源,如果山體滑坡有很多來源,我是否需要將每個來源改爲來源? –

0

您需要使用accept_nested_attributes_for和巢相應表單:(。隨着問候保留什麼樣的形式應該被嵌套在其中,我用源通過滑坡表單提交的例子)

在landslide.rb

accept_nested_attributes_for :sources 

在您看來(我不知道HAML但不管怎麼說)

<%= form_for :landslide do |f|%> 
    <%= f.select :start_date %> 

    <%= fields_for :sources do |s| %> 
     <%= s.input :your_column %> 
    <% end %> 

    <%= f.button :submit %> 
<% end %> 

順便說一下,有很多關於這個已經,這就是所謂的 '嵌套表格'

Nested forms in rails - accessing attribute in has_many relation

Rails -- fields_for not working?

fields_for in rails view