ammmyy
ammmyy

Reputation: 25

can't add create method

I'm adding create action to food_comment controller for saving comment but there is a no method error on browser.

↓Food_comments controller ↓

class FoodCommentsController < ApplicationController

  def create
    @food_comment = current_user.food_comments.build(food_comment_params)
    if @food_comment.save
      redirect_to "/food/#{@food_comment.food.id}"
    end
  end

  private
  def food_comment_params
    params.require(:food).permit(:food_comment).merge(food_id: params[:food_id])
  end
end

show view

  <div class="container">
      <%= form_with(model: [@food_comment,@food], url:food_food_comments_path(@food), method: :post, local: true) do |f| %>
        <%= f.text_area :food_comment, placeholder: "add comment", class:"comment_field food_comment_field" %>
      <div><%= f.submit "send", class:"food_comment_btn" %></div>
      <% end %>
  </div>

food model

class Food < ApplicationRecord
  belongs_to :user
  has_one_attached :food_image
  has_many :food_comments
  
  with_options presence: true do
    validates :food_title
    validates :food_text
  end
end

user model

         has_many :cat_posts, dependent: :destroy
         has_many :cat_post_comments, dependent: :destroy
         has_many :foods, dependent: :destroy
         has_many :food_comments, dependent: :destroy

The error pic on browser

I tried to change method but everything I did was wrong and there were same error. That would be great if someone know how to fix it. I'm looking forward to get some responses, thank you.

it's bit different the error messages after fix user model and food association.

the next error messages

route

  resources :foods do 
    resources :food_comments, only: :create
  end
  

console

Loading development environment (Rails 6.0.3.2)
[1] pry(main)> User.first.food_comments
   (0.8ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
  User Load (0.3ms)  SELECT `users`.* FROM `users` ORDER BY `users`.`id` ASC LIMIT 1
  FoodComment Load (0.5ms)  SELECT `food_comments`.* FROM `food_comments` WHERE `food_comments`.`user_id` = 1
=> []

enter image description here

Upvotes: 1

Views: 56

Answers (1)

user11350468
user11350468

Reputation: 1407

It seems there is an issue with the path you have redirected on the create action, Try the below (Assuming that you are redirecting to food show page):

  def create
    @food_comment = current_user.food_comments.build(food_comment_params)
    if @food_comment.save
      redirect_to food_path(@food_comment.food_id)
    end
  end

Also please consider refactoring your controller like the below:

class FoodCommentsController < ApplicationController
  before_action :set_food

  def create
    @food_comment = @food.food_comments.build(food_comment_params.merge(user_id: current_user.id))
    if @food_comment.save
      redirect_to food_path(params[:food_id])
    else
      # Handle failure as well
    end
  end

  private
  def set_food
    @food = Food.find(params[:food_id])
  end

  def food_comment_params
    params.require(:food).permit(:food_comment)
  end
end

Upvotes: 1

Related Questions