【问题标题】:How to use x_editable_rails for editing comments如何使用 x_editable_rails 来编辑评论
【发布时间】:2017-07-31 19:15:06
【问题描述】:

不知道为什么我在实施 gem x_editable_rails 以允许在不将用户带到单独的编辑页面的情况下编辑 cmets 时收到此 error message

x_editable_rails 演示应用程序显示,也许正确的方法应该是 <%= editable @comment, :content %>,,但因为我在循环 @cmets 实例变量中的所有 cmets,这样做也会引发错误。

编辑

我将代码行从 <%= editable @comments.comment %> 更改为 <%= editable [comment.article, comment], :content, url: edit_article_comment_path(comment.article, comment) %> 现在,它的显示 undefined method `xeditable?' error.

我已经添加了下面的帮助方法并将这个(helper_method :xeditable?)添加到应用程序控制器。 (我不使用cancan,所以我添加了一个虚拟罐头?正如stackoverflow post所建议的那样。

module ApplicationHelper

def xeditable?
  current_user.xeditable?
end

  def can?(role, object)
     true 
  end
end

_cmets.html.erb

<% @comments.each do |comment| %>
  <%= editable [comment.article, comment], :content, url: edit_article_comment_path(comment.article, comment) %>
  <%= link_to "Delete comment", [comment.article, comment], method: :delete %>
  <%= link_to "Edit comment", edit_article_comment_path(comment.article, comment) %>
<% end %>

routes.rb

  resources :articles do
    resources :comments
  end

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-5


    【解决方案1】:

    路由

    您发布的原始错误表明您没有APP_DOMAIN/comments/:id 的路由,您的路由证实了这一点——评论是您路由中的嵌套资源,路由类似于APP_DOMAIN/articles/:id/comments/:id

    请注意,对于不寻常的路由(即不是典型的 Rails 命名模式),x-editable-rails allows you to specify your path。也许 cmets 是通过另一个名称路由的,例如 url: post_critique_path(@comment.post, @comment)。我看到你现在已经这样做了,虽然我不确定是否有必要,因为你的路由是标准做法。

    请注意,我自己没有对此进行测试。 README 对:url 的允许或预期值不是很清楚。而且,令人困惑的是,gem 有一个:nested 选项——which appears to be about setting the title HTML attribute

    应用程序帮助方法

    对于您的下一个错误,您似乎正在实施来自this answer 的建议。但我相信你漏掉了一行,你的 ApplicationHelper 模块应该是这样的:

    module ApplicationHelper
    
      helper_method :xeditable?, :can
    
      def xeditable?
        current_user.xeditable?
      end
    
      def can?(role, object)
         true 
      end
    end
    

    【讨论】:

    • no.. 但我将其更改为 def xeditable? object = nil true end
    • @diana 听起来我的建议可能纠正了您最近的错误,并且您现在看到了一个新错误。如果这是正确的,我建议使用您的问题详细信息提交一个新的 SO 问题,并将此答案标记为正确。如果您仍然看到与以前相同的错误,我建议您将相关详细信息添加到您的答案中,以便其他人提供替代答案。
    猜你喜欢
    • 2015-08-16
    • 1970-01-01
    • 2015-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-05-21
    • 2022-07-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多