"comments", :article_id=>nil, :id=>nil} 用于编辑 ruby​​ on rails 博客中文章的评论-6ren"> "comments", :article_id=>nil, :id=>nil} 用于编辑 ruby​​ on rails 博客中文章的评论-在我决定为评论添加编辑功能之前,我的应用程序运行良好。在此之前,我可以查看特定文章,但现在不能。现在,当我尝试访问有一些评论的特定文章(文章/节目)。实际上,我正在尝试为文章实现评论编辑功能。为此,我-6ren">
gpt4 book ai didi

ruby-on-rails - 没有路由匹配 { :action= >"edit", :controller= >"comments", :article_id=>nil, :id=>nil} 用于编辑 ruby​​ on rails 博客中文章的评论

转载 作者:数据小太阳 更新时间:2023-10-29 08:04:48 25 4
gpt4 key购买 nike

在我决定为评论添加编辑功能之前,我的应用程序运行良好。在此之前,我可以查看特定文章,但现在不能。现在,当我尝试访问有一些评论的特定文章(文章/节目)。实际上,我正在尝试为文章实现评论编辑功能。为此,我在 comments/_comment.html.erb 中放置了评论的“编辑”链接。值得注意的是,只有当文章有评论时我才会收到此错误,而当文章没有评论时,它不会收到。我对文章和评论有不同的看法。

评论 Controller .rb

    class CommentsController < ApplicationController
before_filter :user_signed_in, except: [:create]
def new
@comment = Comment.new
end

def create
@article = Article.find(params[:article_id])
@comment = @article.comments.build(params[:comment])
@comment.user_id = current_user.id
@comment.save
flash[:success] = "Comment created!"
redirect_to article_path(@comment.article)
end

def edit
@comment = Comment.find(params[:id])
end

def update
@comment = Comment.find(params[:id])
@article = @comment.article
respond_to do |format|
if @comment.update_attributes(params[:comment])
redirect_to @article_path(@article)
else
render :action => "edit"
end
end

def destroy
@comment = Comment.find(params[:id])
@article = Article.find(params[:article_id])
@comment.destroy
redirect_to @article_path(@artilce)
end

end

评论/edit.html.erb

    <h3>Editing comment</h3>
<%= render :partial => 'comment_form' %>

评论/_comment_form.html.erb

    <%= form_for ([@article, @article.comments.build]), :required => true do |f| %>
<%= f.hidden_field :article_id %>
<%= f.text_area :content, :style => "width:727px; height:100px; border: 1px solid #999999;margin-top:80px; background-color:#FFFFFF;margin-left:-33px" %>
<div class="actions">
<%= f.submit "Add Comment", :style => "margin-right:20px; margin-left:560px; background-color:#66C9Ef; color:#FFFFFF; border: 0px solid #82b548; border-radius: 3px 3px 3px 3px; font-size: 1.3rem;" %>
</div>
<% end %>

comments/comment.html.erb(这里我给出了编辑文章评论的链接)

    <% if @article.comments.count  >= 1 %>
<div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding: 10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;">
<%= comment.content %>
<div id="tabula">
<ul id="tabula">
<li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article,comment) %> </div></li>
</ul>
</div>
</div>
<% else %>
<div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div>
<% end %>

rake 路线结果。(不完整)

            articles GET    /articles(.:format)                               articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
dashboard_index GET /dashboard(.:format) dashboard#index
POST /dashboard(.:format) dashboard#create
new_dashboard GET /dashboard/new(.:format) dashboard#new
edit_dashboard GET /dashboard/:id/edit(.:format) dashboard#edit
dashboard GET /dashboard/:id(.:format) dashboard#show
PUT /dashboard/:id(.:format) dashboard#update
DELETE /dashboard/:id(.:format) dashboard#destroy


tags GET /tags(.:format) tags#index
POST /tags(.:format) tags#create
new_tag GET /tags/new(.:format) tags#new
edit_tag GET /tags/:id/edit(.:format) tags#edit
tag GET /tags/:id(.:format) tags#show
PUT /tags/:id(.:format) tags#update
DELETE /tags/:id(.:format) tags#destroy
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
GET /articles/new(.:format) articles#new
GET /articles/:id/edit(.:format) articles#edit
GET /articles/:id(.:format) articles#show
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy

路线.rb

    Mau::Application.routes.draw do
devise_for :users
root :to => 'articles#index'
resources :articles
resources :dashboard
resources :tags
resources :articles do
resources :comments
end
end

comments_comment.html.erb

<% if @article.comments.count  >= 1 %>
<div style="border: px solid #66c9ee;border-radius: 0px 0px 0px 0px;margin: 10px -30px 15px; padding: 10px 15px 25px; background: none repeat scroll 0 0 #F2F2F2; width:700px; font-size: 1.2em;border-bottom: 0px solid #DDDDDD;">
<%= comment.content %>
<div id="tabula">
<ul id="tabula">
<li> <div style="color: #0077CC;margin-rigth:200px; font-size: 1.0em;margin-top:4px;background-color:#;"> <%= comment.user.username if comment.user %></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <p> <%= time_ago_in_words(comment.created_at.in_time_zone("Asia/Calcutta")) unless comment.created_at.nil? %> </p></div></li>
<li> <div style="color: #0077CC; background-color:; margin-top:4px; margin-left:25px;"> <%= link_to "edit", edit_article_comment_path(@article ,@comment) %> </div></li>
</ul>
</div>
</div>
<% else %>
<div style="color:#0077CC;margin-left:25px;font-size:1.4em;"> be first to comment</div>
<% end %>

最佳答案

<% if @article.comments.count  >= 1 %>
<div "...">
<%= comment.content %> # <-- This.

我怀疑 <%= comment.content %>及其他commentcomments_comment.html.erbcomments/comment.html.erb 中找到的变量。

对我来说似乎是合理的,因为如果相关文章没有评论,您不会收到错误,如 @article.comments.count >= 1返回 false .

有一个工作comment变量,在 if 语句后插入一个循环:

<% if @article.comments.count  >= 1 %>
<div "...">
<% @article.comments.each do |comment| %> # <-- This.
<%= comment.content %>
...
<% end %> # Remember to close the loop.
</div>
<% else %>
...

此外,请注意使用 comment而不是 @commentedit_article_comment_pathcomment/_comment.html.erb 中。

关于ruby-on-rails - 没有路由匹配 { :action= >"edit", :controller= >"comments", :article_id=>nil, :id=>nil} 用于编辑 ruby​​ on rails 博客中文章的评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16190178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com