Ruby on rails 3 ActiveRecord::RecordNotFound无法';找不到没有ID的注释
在我的评论视图页面(show.html)上,我有一个按钮,单击该按钮时,将在另一个名为Repost的表中创建一行(它有一个reposts\u控制器和一个Repost模型)。但是,当我单击“评论显示”页面上的图标时,出现以下错误:Ruby on rails 3 ActiveRecord::RecordNotFound无法';找不到没有ID的注释,ruby-on-rails-3,Ruby On Rails 3,在我的评论视图页面(show.html)上,我有一个按钮,单击该按钮时,将在另一个名为Repost的表中创建一行(它有一个reposts\u控制器和一个Repost模型)。但是,当我单击“评论显示”页面上的图标时,出现以下错误: Couldn't find Comment without an ID 这是RepostsController: class RepostsController < ApplicationController def create @comment =
Couldn't find Comment without an ID
这是RepostsController:
class RepostsController < ApplicationController
def create
@comment = Comment.find(params[:id])
@repost = @comment.reposts.build(params[:comment])
@repost.user = current_user
if @repost.save
#flash[:success] = "Post created!"
redirect_to root_url
else
render 'activities'
end
end
end
class RepostsController
我知道问题是什么,但我正在努力寻找解决办法。CommentsController可以轻松找到要显示在显示页面上的注释。但是,如何将该id从show页面发送到RepostController,以便它可以创建上面所示的所需关系?这是一种奇怪的情况,因为该按钮位于实际的注释页面上,但由于该按钮使用不同的控制器将一行写入不同的表,因此该按钮无法查看/查找注释
谢谢!
-b
新增视图:
<%= link_to image_tag('star.png', :size => "16x16", :align => "right"), {:controller => 'reposts', :action => 'create', :id => @comment} %>
“16x16”,:align=>“right”),{:controller=>'reposts',:action=>'create',:id=>@comment}%>
指向的链接将我发送到localhost/reposts?id=1,并且在reposts表上没有创建任何内容。以下代码结束工作:
class RepostsController < ApplicationController
def index
@reposts = Repost.all
end
def create
@repost= Repost.new("comment_id" => params[:comment_id],"user_id" => params[:user_id])
@content = @repost.comments.content
if @repost.save
#flash[:success] = "Post created!"
redirect_to root_url
else
render 'activities'
end
end
end
<%= link_to image_tag('purple_star.png', :size => "16x16", :align => "right"), {:controller => 'reposts', :action => 'create', :comment_id => @comment, :user_id => @comment.user}, :title=> "Pass it on!", :method=>'post' %>
class RepostsControllerparams[:注释id],“用户id”=>params[:用户id])
@content=@repost.comments.content
如果@repost.save
#flash[:success]=“创建帖子!”
将\u重定向到根\u url
其他的
呈现“活动”
结束
结束
结束
“16x16”,:align=>“right”),{:controller=>“reposts”,:action=>“create”,:comment\u id=>@comment,:user\u id=>@comment.user},:title=>“继续传递!”,:method=>“post”%>
谢谢大家的时间和帮助 我们可以看到视图代码中出现链接的部分吗?您是否正在从视图中将
@comment
(或comment id)传递给控制器?感谢您的回复!刚刚添加了视图代码。我最初在表单中使用一个按钮,这就是导致我出错的原因。我尝试了另一次尝试的链接,但它只是将我发送到一个空白页,并且数据库中没有创建任何行。该链接对用户的用途是什么?是创建评论还是创建转载?对不起,应该更具体一些。链接将需要创建一个repost,但将注释id和注释的用户id保存为repost表中的外键。要在repost表中创建内容,您需要一个@repost=repost.new(params[:repost])
作为创建中的第一行。您还需要将@repost=@comment.repost…
替换为@comment.repost