Доступ к типу STI при создании представления и контроллере - PullRequest
1 голос
/ 21 ноября 2011

Здравствуйте, ребята, у меня 2 клиента модели и еда.

client.rb

class Client < ActiveRecord::Base

has_many :meals
accepts_nested_attributes_for :meals

end

meal.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

1 Ответ

0 голосов
/ 01 декабря 2011

Дело в том, что я просто должен установить наследование столбцов в food.rb следующим образом:

class Meal < ActiveRecord::Base

    set_inheritance_column do
        "type" + "_id"
    end 

belongs_to :client
end

class Lunch < Meal
end

class Dessert < Meal
end

Так что теперь я могу выбрать тип еды, когда создаю клиента.Благодаря Анану решение исходит от него.

...