Ruby on rails can';不添加创建方法
我正在将创建操作添加到food_comment controller以保存注释,但浏览器上没有方法错误 ↓食物控制员↓Ruby on rails can';不添加创建方法,ruby-on-rails,ruby,Ruby On Rails,Ruby,我正在将创建操作添加到food_comment controller以保存注释,但浏览器上没有方法错误 ↓食物控制员↓ class FoodCommentsController < ApplicationController def create @food_comment = current_user.food_comments.build(food_comment_params) if @food_comment.save redirect_to &q
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
我试图改变方法,但我所做的一切都是错误的,也有同样的错误。
如果有人知道如何修复它,那就太好了。我期待着得到一些回应,谢谢
修复用户模型和食品关联后的错误消息略有不同
路线
控制台
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
=> []
您在“创建”操作中重定向的路径似乎有问题,请尝试以下操作(假设您正在重定向到“食品展示”页面): 请考虑重构您的控制器,如下所示:
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
class FoodCommentsController
用户模型上似乎没有定义关联食品注释,您能否在用户模型上共享代码?非常抱歉,我没有注意到我得到了响应,你是对的,我没有定义与用户模型的关联并修复它,但浏览器上有相同的错误消息…你能从rails控制台访问食物注释吗,比如user。首先。食物注释?你的新操作在哪里?在没有新操作的情况下,您如何进行create
操作?你的路线也有点滑稽:<代码> FooTooFooCysTysPix——也许重命名它。考虑嵌套你的路线吗?请添加您的路线文件-有人将能够再次感谢您!!这很有帮助!!
has_many :cat_posts, dependent: :destroy
has_many :cat_post_comments, dependent: :destroy
has_many :foods, dependent: :destroy
has_many :food_comments, dependent: :destroy
resources :foods do
resources :food_comments, only: :create
end
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
=> []
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
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