May you help me again please? I want to pass day\_id when I create new dish. I make dishes controller and view form\_for. By clicking button in view i want to write the value of @day.id in db table ‘courses’ in attribute day\_id.

P.S. in ‘days’ table the name of attribute is id.
In course table the name of attribute is day\_id

**Course migration is:**

```
class CreateCourses < ActiveRecord::Migration[6.0] 
  def change
     create_table :courses do |t|
       t.belongs_to :day, index: true 
      t.belongs_to :dish, index: true 
      t.timestamps 
    end
   end 
end

```

**Models:**

```
class Day < ApplicationRecord
   has_many :courses 
  has_many :dishes, through: :courses
   has_many :order_items
 end

```

```
class Dish < ApplicationRecord 
  has_many :courses
   has_many :days, through: :courses
   has_many :order_items 
  accepts_nested_attributes_for :courses
 end

```

```
class Course < ApplicationRecord 
  belongs_to :day 
  belongs_to :dish 
end

```

**Dishes controller:**

```
class DishesController < ApplicationController  
  def new
     @dish = Dish.new
     @dish.courses.build 
    @days = @dish.days
     @day = Day.find_by_id(params[:id]) 
  end

 def create 
    @dish = Dish.new(dish_params) 
    if @dish.save!
       redirect_to '/dashboard' 
    else 
      render 'new' 
    end
   end

private def dish_params
     params.require(:dish).permit(:id, :name, :description, :price, courses_attributes: [:id, :day_id, :name]) 
  end  
end

```

**View**

```
<div class="new dish">
   <%= form_for @dish do |d|  %>
   <%= d.text_field :name, :placeholder => "Dish name" %> 
  <%= d.text_area :description, :placeholder => "Description" %> 
  <%= d.text_field :price, :placeholder => "Dish price" %>
   <%= d.fields_for :courses do |c| %> 
      <%= c.text_field :name, :placeholder => "Course name" %> 
      <%= c.hidden_field :day_id, value: @day.id %>
   <%= d.submit "Save" %> 
</div> 
<% end %> 
<% end %>

```

**Log**

```
Started GET "/dishes/new" for ::1 at 2020-10-20 12:59:34 +0300
Processing by DishesController#new as HTML
  Day Load (0.2ms)  SELECT "days".* FROM "days" WHERE "days"."id" IS NULL LIMIT ?  [["LIMIT", 1]]
  ↳ app/controllers/dishes_controller.rb:7:in `new'
  Rendering dishes/new.html.erb within layouts/application
  Rendered dishes/new.html.erb within layouts/application (Duration: 2.9ms | Allocations: 1942)
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.2ms | Allocations: 4393)

ActionView::Template::Error (undefined method `id' for nil:NilClass):
     5:   <%= d.text_field :price, :placeholder => "Dish price" %>
     6:   <%= d.fields_for :courses do |c| %>
     7:       <%= c.text_field :name, :placeholder => "Course name" %>
     8:       <%= c.hidden_field :day_id, value: @day.id %>
     9:   <%= d.submit "Save" %>
    10: </div>
    11: <% end %>

app/views/dishes/new.html.erb:8
app/views/dishes/new.html.erb:6
app/views/dishes/new.html.erb:2

```