大家好,我有一个 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