簡體   English   中英

Rails - 一種形式到兩種模式

[英]Rails - one form to two models

我正在Rails(版本4.2.5)中構建一個表單來收集有關商店的基本信息,我希望將表單提交給兩個單獨的模型 - 一個名為Store,它收集有關商店的信息,一個名為Address ,它只保存地址信息。 我已經看到這個問題浮在Stack和其他地方,但我對Rails很新,可以使用更多的逐步指導。 (另外,我知道Rails地理編碼器的寶石,但不想在這個項目中使用它,盡管我正在收集地址和緯度/長信息)

對於初學者,這是我的商店和地址模型......

class Store < ActiveRecord::Base
  belongs_to :user
  belongs_to :store_type
  has_one :address
end

class Address < ActiveRecord::Base
  belongs_to :store
end

和我的StoresController ......

class StoresController < ApplicationController

    def new
        @store = Store.new
        @storeType = StoreType.all
    end

    def create
        @store = Store.new(store_params)
        @store.user = current_user

        if @store.save
            flash[:notice] = "New store created"
            redirect_to root_path
        else
            #ERROR STUFF
        end
    end

    private
    def store_params
        params.require(:store).permit(:user_id, :store_type_id, :latitude, :longitude, :name, :notes)
    end
end

最后,創建新商店的表格(不包括地址)......

<%= form_for @store do |f| %>

    <%= f.hidden_field :latitude, :id => "latitude_field"  %>
    <%= f.hidden_field :longitude, :id => "longitude_field"  %>

    <div class="field">
        <%= f.label :store_type_id %>
        <%= f.select :store_type_id, @storeType.map{ |type| [type.store_type.capitalize, type.id] } %>
    </div>

    <div class="field">
        <%= f.label :name %>
        <%= f.text_field :name  %>
    </div>

    <div class="field">
        <%= f.label :notes %>
        <%= f.text_field :notes  %>
    </div>

    <div class="actions">
        <%= f.submit :"Submit" %>
    </div>
<% end %>

因此,完成的表單將上述字段傳遞給Store模型,還應包括street,city,state和zip的字段,這些字段將傳遞給Address模型。

總的來說,最好的辦法是什么?

並且,作為后續問題,我的地址模型有一個store_id列 - 如果正在創建存儲和地址模型從同一表單填充,此列是否會自動填充商店的ID? 還是我在這里退出了聯盟?

謝謝。

您可以在Store模型中使用accept_nested_attributes_for :address ,然后使用fields_for實例化fields_for

像這樣的東西:

class Store < ActiveRecord::Base
  has_one :address
  accept_nested_attributes_for :address
end

class StoresController < ApplicationController

  def new
    @store = Store.new
    @store.build_address
  end
end

<%= form_for @store do |f| %>
  <%= f.fields_for @store.address do |ff| %>

您還需要在StoreController中將address_attributes列入白名單,並在保存Store對象后保存地址。

您最好關注accepts_nested_attributes_for

#app/models/store.rb
class Store < ActiveRecord::Base
   has_one :address
   accepts_nested_attributes_for :address
end

#app/controllers/stores_controller.rb
class StoresController < ApplicationController
   def new
      @store = current_user.stores.new
      @store.build_address
   end

   def create
      @store = current_user.stores.new store_params
      @store.save
   end

   private

   def store_params
      params.require(:store).permit(:store_type_id, :latitude, :longitude, :name, :notes, address_attributes: [:line_1])
   end
end

#app/views/stores/new.html.erb
<%= form_for @store do |f| %>
   <%= f.fields_for :address do |a| %>
      <%= a.text_field :line_1 %>
   <% end %>
   <%= f.submit %>
<% end %>

暫無
暫無

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

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