How to create nested form in Ruby on Rails?

孤街浪徒 提交于 2019-12-11 18:38:00

问题


I have following arragement

The Pizza model, to create a list of pizzas that can be ordered by customers, also gets associated with order, so to indicate which pizza has been ordered.

class Pizza < ActiveRecord::Base  
  has_many :pizza_orders
  has_many :orders, :through => :pizza_orders
  has_and_belongs_to_many :toppings
end

Option model, to create a list of options that can be associated with certain pizzas, also gets associated with the join table for each pizza order, to specify which pizza has the topping ordered.

class Topping < ActiveRecord::Base
  has_and_belongs_to_many :pizzas
  has_and_belongs_to_many :pizza_orders
end

The join table for pizza and topping, this is needed because without it, you can not specify which toppings can or can not be ordered with a pizza. After all, having peperoni topping listed to a vegetarian pizza might offend someone.

class PizzasToppings < ActiveRecord::Base
  belongs_to :pizza
  belongs_to :topping
end

The order model, this just holds all the join tables together.

class Order < ActiveRecord::Base
  has_many :pizza_orders
  has_many :pizzas, :through => :pizza_orders
end

The join table between pizza and order, this many to many is a has many through, and not a has and belongs to many since in Rails you cannot directly manipulate HBATM join tables (as far as I have tried), and you need to be able to because of the options relation ship.

class PizzaOrder < ActiveRecord::Base
  belongs_to :pizza
  belongs_to :order

  has_and_belongs_to_many :toppings
end

The join table to indicate which toppings have been chosen for a particular pizza in a order.

class PizzaOrdersToppings < ActiveRecord::Base
  belongs_to :pizza_orders
  belongs_to :topping
end

Then I have an admin page to create and associate the Pizzas and toppings.

But I don't know how to create the orders form. The user should be able to add a pizza and select one or many toppings which are already created


回答1:


Its a bit old screencast Nested model form part 1 made by ryan bates but i hope it helps you , also there is a revised version of this screencast.




回答2:


Sorry I was late to respond.

Okay, haven't tested this, but hope it gets you the idea. In order for it to work, this sample relies on,

  1. simple_form
  2. the ryan bates dynamic nested form

I have to apologize, this answer is lacking a large part of it, you need to re-render the link_to_add_fields part every time you change the <%= f.association :pizzas %> value so that the hidden field that contains the toppings match the pizza that has been selected.

Sorry I couldn't get you completly through, but I hope this points you in the right direction.

_form.html.erb

<%= simple_form @order do |f| %>

  <%= f.simple_fields_for :pizza_orders do |pizza_orders_fields| %>
    <%= f.association :pizzas %>
    <%= render partial: "pizza_order_fields", locals: {f: pizza_orders_fields}%>
    <%= link_to_add_fields "Add", f, :pizza_orders %>
  <% end %>
<% end %>

pizza_order_fields.html.erb would look like

<fieldset>
  <%= f.hidden_field :_destroy %>
  <%= link_to "Delete", '#', class: "remove_fields btn btn-danger" %>
  <%= f.association toppings_pizza_orders, collection: f.object.pizza.toppings %>
</fieldset>


来源:https://stackoverflow.com/questions/18641079/how-to-create-nested-form-in-ruby-on-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!