多态关联路线倒退

时间:2017-05-08 19:58:48

标签: ruby-on-rails

我的多态关联存在问题。使用此代码<%= link_to "Edit", edit_blog_comment_path(comment) %>时,编辑链接会在我的视图页面中显示。我正在尝试启用评论的编辑,在这种情况下是一个属于博客的评论。但是,链接实际上指向.../blogs/7/comments/3/edit,而它应该是相反的。博客ID为3,评论ID为7.这是所有评论的方式。我找不到错误的位置。是什么导致他们交换?

我的路线:

blog_comments GET    /blogs/:blog_id/comments(.:format)          comments#index
                         POST   /blogs/:blog_id/comments(.:format)          comments#create
        new_blog_comment GET    /blogs/:blog_id/comments/new(.:format)      comments#new
       edit_blog_comment GET    /blogs/:blog_id/comments/:id/edit(.:format) comments#edit
            blog_comment GET    /blogs/:blog_id/comments/:id(.:format)      comments#show
                         PATCH  /blogs/:blog_id/comments/:id(.:format)      comments#update
                         PUT    /blogs/:blog_id/comments/:id(.:format)      comments#update
                         DELETE /blogs/:blog_id/comments/:id(.:format)      comments#destroy
                   blogs GET    /blogs(.:format)                            blogs#index
                         POST   /blogs(.:format)                            blogs#create
                new_blog GET    /blogs/new(.:format)                        blogs#new
               edit_blog GET    /blogs/:id/edit(.:format)                   blogs#edit
                    blog GET    /blogs/:id(.:format)                        blogs#show
                         PATCH  /blogs/:id(.:format)                        blogs#update
                         PUT    /blogs/:id(.:format)                        blogs#update
                         DELETE /blogs/:id(.:format)                        blogs#destroy

我的评论控制器

class CommentsController < ApplicationController
  before_action :load_commentable
  before_action :authenticate_user!, except: [:index, :show]

  def index
    @comments = @commentable.comments
  end

  def edit
  end

  private

  def load_commentable
    resource, id = request.path.split('/')[1,2]
    @commentable = resource.singularize.classify.constantize.find(id)
  end

  def allowed_params
    params.require(:comment).permit(:content, :user_id)
  end
end

我的_comments.html.erb。如果下半部分看起来很难看,那是因为我不能在我的生活中得到@commentable变量来处理编辑和删除路径所以我只是做了一个条件语句。

<div id="articles-wrapper">
    <% @comments.each do |comment| %>
      <div class="articles">
        <div class="post-title"><%= comment.content.html_safe %></div>
        <div class="date">Posted <%= time_ago_in_words(comment.created_at) %> ago by <%= link_to comment.user.username, user_path(comment.user) %>

          <% if @commentable = @blog %>
            <%= link_to edit_blog_comment_path(comment) do %><i class="fa fa-pencil"></i><% end %>
            <%= link_to blog_comment_path(comment), method: :delete, data: { confirm: "Are you sure?" } do %><i class="fa fa-times"></i><% end %>
          <% else %>
            <%= link_to edit_article_comment_path(comment) do %><i class="fa fa-pencil"></i><% end %>
        <%= link_to article_comment_path(comment), method: :delete, data: { confirm: "Are you sure?" } do %><i class="fa fa-times"></i><% end %>
          <% end %>

        </div>
      </div>
    <% end %>
</div>

1 个答案:

答案 0 :(得分:0)

更改视图文件中的link_to以包含两个元素,而不仅仅包含元素。

<%= link_to edit_blog_comment_path(@blog, comment) do %>

然后在注释控制器中确保注释实例变量的设置与您使用它的方法类似。

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