1

I'm learning Rails building an ordering system and I'm stuck trying to build a form for Orders. Where Orders is a nested resource for Restaurant. Routes:

resources :restaurants do
    resources :orders
    resources :recipes
end

My models look like this:

class Restaurant < ActiveRecord::Base
    has_many :orders
    has_many :recipes, dependent: :destroy
end

class Order < ActiveRecord::Base
    belongs_to :restaurant
    has_many :order_recipes, dependent: :destroy
    has_many :recipes, through: :order_recipes
end

class Recipe < ActiveRecord::Base
    belongs_to :restaurant
    has_many :order_recipes
    has_many :orders, through: :order_recipes
end

My controller:

        @recipes = @restaurant.recipes
        @order_recipes = @recipes.map{|r| @order.order_recipes.build(recipe: r)}

And my view:

<%= form_for([@restaurant, @order]) do |order_form| %>
        <%= order_form.label :Table_Number %>
        <%= order_form.number_field :table_id %>

        <%= order_form.fields_for :order_recipes, @order_recipes do |orf| %>
        <%= order_form.hidden_field :recipe_ids %>
        <%= order_form.label Recipe.where(id: :recipe_id) %>
        <%= orf.number_field :quantity %>

My current problem is displaying the names of each recipe. It seems that :recipe_id is being passed as null all the time. My ultimate goal is to be able to build order_recipes populating the quantity column, and I thought having the recipe_id from order_recipes I could also access the correct recipe object from the DB to display the name or any other relevant data.

Patricio Jerí
  • 528
  • 2
  • 5
  • 15
  • Did you forget the OrderRecipes model? – Vasseurth Aug 02 '15 at 16:29
  • Nope, its in there. Btw, this answer works for me, at least for the form. Still struggling with the parameter passing.(http://stackoverflow.com/questions/25235025/rails-4-accessing-join-table-attributes) – Patricio Jerí Aug 02 '15 at 16:31

3 Answers3

0

I assumed, you have a restaurant, it has recipes, it takes orders, orders are tracked by order_recipes table.

# controller
@recipes = @restaurant.recipes
@order = @recipes.map{|r| r.order.build}

# view
<%= form_for([@restaurant, @order]) do |order_form| %>
  ...
  <% @order.each do |index, ord| %>
    <%= order_form.fields_for :orders, @order do |orf| %>
      ...
      <%= order_form.label @recipes[index] %>
      <%= orf.number_field :quantity %>

# also in restaurant model
accepts_nested_attributes_for :orders
Ojash
  • 1,234
  • 8
  • 20
  • Sorry, forgot to include that line. Edited above to reflect actual code. – Patricio Jerí Aug 02 '15 at 15:59
  • Could you also post your routes file. – Ojash Aug 02 '15 at 16:14
  • Edited Question - But its just a normal nested resource. :) Also, I've started to use the answer provided in this question, but to no success yet: http://stackoverflow.com/questions/25235025/rails-4-accessing-join-table-attributes – Patricio Jerí Aug 02 '15 at 16:17
0

try so in your controller:

@order_recipes = @recipes.map{|r| @order.build_order_recipes(recipe: r)}
0

Eventually I got this the answer posted in this question to work: Rails 4 Accessing Join Table Attributes I'm only struggling with passing the correct parameters now, but the form is correct.

Community
  • 1
  • 1
Patricio Jerí
  • 528
  • 2
  • 5
  • 15