嵌套编辑路径不工作
试图找出_comment.html.erb
文件中的编辑路径。嵌套编辑路径不工作
不断收到此错误:
ActiveRecord::RecordNotFound in CommentsController#edit
Couldn't find Article with 'id'=#<Comment::ActiveRecord_Associations_CollectionProxy:0x007fcac37359e8>
不知道如何计算如何写正确的路径。
评论控制器
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
_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>
路线
resources :articles do
resources :comments
end
怎么办我写出正确的道路?
而且,早期的路我已经是这样的:
<%= link_to 'Edit', edit_article_comment_path(@article, comment) %>
但它会变成一个空白的编辑形式,即,没有一个文本框有任何填写...因此为什么我试过其他路径。
任何帮助,将不胜感激。
谢谢。
评论_form.html.erb
<%= 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 %>
文章show.html.erb
<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 %>
有一个在_comment.html.erb问题
<%= link_to 'Edit', edit_article_comment_path(@article.comments, comment) %>
应该
<%= link_to 'Edit', edit_article_comment_path(@article, comment) %>
But it would turn up a blank edit form, ie, none of the text boxes had anything filled in
如果传递正确的价值观在form_for
<%= form_for [@article, @comment] do |f| %>
EDIT2
您应该检查井的问题是要传递的新实例每次评论,即使是编辑操作。这就是为什么你没有得到形式的任何值
<%= form_for([@article, @article.comments.build]) do |f| %>
将其更改为
<%= form_for([@article, @comment]) do |f| %>
并确保你在CommentsController
都new
和edit
行动或无论你在哪里分配@article
和@comment
渲染comments/_form
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
已经用评论的_form.html.erb更新了我的帖子...我现在就尝试过你了,并且我得到了这个错误:'表单中的第一个参数不能包含零或者是空的参考你的行......有任何想法吗?谢谢! – user273072545345
雅,你是第二个例子更准确。你最初的错误是说当你期待一个单一的对象时你传递了一个对象数组。我可以看到你用来渲染你的部分代码吗? – Anthony
@Anthony,抱歉,不完全确定你的意思......我的意思是,我知道你的意思,但是...无论如何,还包括文章的演出文件...这是一个呈现评论...让我知道你的意思,否则。 – user273072545345