1

大家好,我有一个 2 模型客户和膳食。

客户端.rb

class Client < ActiveRecord::Base

has_many :meals
accepts_nested_attributes_for :meals

end

膳食.rb

class Meal < ActiveRecord::Base
belongs_to :client
end

class Lunch < Meal
end

class Dessert < Meal
end

意见/客户/_form.html.erb

    <%= simple_form_for @client do |f| %>

    <%=f.input :name %>
    <%=f.input :adress %>
    <%=f.input :telephone %>

   <%= f.simple_fields_for :meal do |m| %>
    <%=m.input :type %>
    <%end%>
  <% end %>

当我保存膳食类型时,它不会出现在客户端的 index.html.erb 上(它是空白的)。问题是什么?如何通过使用以下控制器为他提供膳食类型(例如“午餐”)来创建客户:

def create
  @client = Client.new(params[:client])

  respond_to do |format|
  if @client.save
    format.html { redirect_to @client, notice: 'Operation was successfully created.' }
    format.json { render json: @client, status: :created, location: @client }
  else
    format.html { render action: "new" }
    format.json { render json: @client.errors, status: :unprocessable_entity }
  end
  end
end
4

1 回答 1

0

问题我只需要像这样在meal.rb中设置列继承:

class Meal < ActiveRecord::Base

    set_inheritance_column do
        "type" + "_id"
    end 

belongs_to :client
end

class Lunch < Meal
end

class Dessert < Meal
end

所以现在我可以在创建客户端时选择餐食类型。感谢Anan解决方案来自他。

于 2011-12-01T12:58:17.897 回答