0

I want to insert into two database tables by clicking on a button. And there are several problems:

First Problem:

Models:

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

Controller:

class DishesController < ApplicationController

  
def new
    
@dish = Dish.new
    
@dish.courses.build
  
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, :name])

  end


end

Views:

<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" %>

  <%= d.submit "Save" %>
</div>

<% end %>

<% end %>

After pressing the Save button, there is no insertion into the database. no redirection to '/ dashboard' occurs, the form is rendered as written in the controller. Here is the log:

Processing by DishesController#create as HTML
  Parameters: {"authenticity_token"=>"fNbaoB4w+OINn17HQ0CdwAothP8PTzR7XPYy4oy5kHpFiCUOXY4T2hOFMbrmObXFaVdI6R1jPE2/K+piXPvymQ==", "dish"=>{"name"=>"aa", "description"=>"aaa", "price"=>"1", "courses_attributes"=>{"0"=>{"name"=>"main course"}}}, "commit"=>"Save"}
  Rendering dishes/new.html.erb within layouts/application
  Rendered dishes/new.html.erb within layouts/application (Duration: 2.2ms | Allocations: 464)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 32ms (Views: 21.5ms | ActiveRecord: 0.0ms | Allocations: 6607)

If I change the view to:

<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_attributes do |c| %>

      <%= c.text_field :name, :placeholder => "Course name" %>

  <%= d.submit "Save" %>

</div>

<% end %>

<% end %>

then I get

TypeError (no implicit conversion of Symbol into Integer):
app/controllers/dishes_controller.rb:9:in `create'

And here is a log:

Processing by DishesController#create as HTML
  Parameters: {"authenticity_token"=>"jE7DMuPcyEo403MKrxjO2KySlM3cA/t9ZjGYNnAUlAu1EDycoGIjcibJHHcKYebdz+hY284v80uF7EC2oFb26A==", "dish"=>{"name"=>"xxx", "description"=>"tt", "price"=>"1", "courses_attributes"=>{"name"=>"first course"}}, "commit"=>"Save"}
Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms | Allocations: 934)

The second problem is at the level of theory and I have not yet begun to solve it. I do not yet understand how, without filling the form with values after clicking the button, throw the dish_id and day_id attribute values into the courses table. In the course table, I have several columns: dish_id, day_id, name.

May you help me please? Why there is no insert into two tables in first problem?

2
  • 1
    From the code you have right now, it seems like the first version doesn't get saved because the day_id is missing for the course. belongs_to has a presence validation and since you're not passing a day id it is not valid (or if you have any other validations that you have not posted yet) . You can verify if you change .save to .save!. it'll throw an error Commented Oct 19, 2020 at 16:55
  • @Clara, thank you. I'll try to pass day_id Commented Oct 19, 2020 at 20:19

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.