Ruby on rails 嵌套的编辑路径不工作
试图找出Ruby on rails 嵌套的编辑路径不工作,ruby-on-rails,ruby,Ruby On Rails,Ruby,试图找出\u comment.html.erb文件中的编辑路径 继续获取此错误: ActiveRecord::RecordNotFound in CommentsController#edit Couldn't find Article with 'id'=#<Comment::ActiveRecord_Associations_CollectionProxy:0x007fcac37359e8> 如何编写正确的路径 此外,我之前的做法是: <%= link_to 'Edi
\u comment.html.erb
文件中的编辑路径
继续获取此错误:
ActiveRecord::RecordNotFound in CommentsController#edit
Couldn't find Article with 'id'=#<Comment::ActiveRecord_Associations_CollectionProxy:0x007fcac37359e8>
如何编写正确的路径
此外,我之前的做法是:
<%= link_to 'Edit', edit_article_comment_path(@article, comment) %>
但是它会出现一个空白的编辑表单,也就是说,没有一个文本框填写了任何内容。。。因此我尝试了另一条路
任何帮助都将不胜感激
多谢各位
Comment\u form.html.erb
class CommentsController < ApplicationController
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
def show
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
end
def edit
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
<%= form_for([@article, @article.comments.build]) do |f| %>
<p>
<%= f.label :commenter %><br>
<%= f.text_field :commenter %>
</p>
<p>
<%= f.label :body %><br>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
Article show.html.erb
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %>
</p>
<p>
<%= link_to 'Show', [comment.article, comment] %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
<%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %>
标题:
文本:
评论
添加评论:
|
comment.html.erb中出现问题
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %>
</p>
<p>
<%= link_to 'Show', [comment.article, comment] %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
<%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %>
EDIT2
问题是,您每次都要传递新的注释实例,即使使用编辑操作也是如此。这就是为什么表单中没有任何值
<%= form_for([@article, @article.comments.build]) do |f| %>
是的,第二个例子更准确。您最初的错误是,当需要单个对象时,您正在传递一个对象数组。我可以看一下你用来渲染部分内容的代码吗?@Anthony,对不起,我不太清楚你的意思。。。我是说,我知道你的意思,但是。。。无论如何,也包括了文章的展示文件。。。这就是呈现评论的那个。。。如果你不是故意的,请告诉我。我已经用评论的
\u form.html.erb
更新了我的帖子。。。我现在尝试了你的,我得到了这个错误:表单中的第一个参数不能包含nil,也不能是空的
引用你的行。。。有什么想法吗?非常感谢。
<%= form_for([@article, @comment]) do |f| %>
class CommentsController < ApplicationController
def new
@article = Article.find(params[:article_id])
@comment = @article.comments.new
end
def edit
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
end
#...
end