【问题标题】:respond_with Undefined methodrespond_with 未定义的方法
【发布时间】:2014-12-19 13:26:30
【问题描述】:

我目前是第一次使用 respond_with 和 respond_to 方法。我的控制器文件如下所示:

class CommentsController < ApplicationController
  respond_to :html, :js

  def create
    @post = Post.find(params[:post_id])
    @comment = current_user.comments.build(comment_params)
    @comment.post = @post
    @comment.user = current_user
    authorize @post
    if @comment.save
      flash[:notice] = "Comment was saved."
      redirect_to [@post.topic, @post]
    else
      flash[:error] = "There was an error saving the comment. Please try again."
      redirect_to [@post.topic, @post]
    end
  end

  def destroy
    @post = Post.find(params[:post_id])
    @comment = @post.comments.find(params[:id])

    authorize @comment
    if @comment.destroy
      flash[:notice] = "Comment was removed."
    else
      flash[:error] = "Comment couldn't be deleted. Try again."
    end
  end

  respond_with(@comment) do |format|
    format.html { redirect_to [post.topic, @post] }
  end

private

  def comment_params
    params.require(:comment).permit(:body)
  end
end

我收到错误消息“RoutingError: Undefined Method respond_with”。

我用谷歌搜索了“respond_with”,在 Rails 指南中说我需要一个名为“responders”的 gem,这是有道理的。但是,当我尝试添加它时,我遇到了麻烦。显然,“responders”需要另一个 gem,“railties”,而 rails 需要该 gem 的不同版本。我的 rails 在没有“railties”的情况下工作得很好,至少到现在为止。

有谁知道我是否真的需要“响应者”,或者还有什么可能导致我的错误消息,或者我应该安装哪个版本的“railties”(如果有的话)?

谢谢!

【问题讨论】:

  • 响应者 gem 仅适用于 Rails 4.x,您使用的是哪个版本的 Rails?
  • 我使用的是 rails 4.0.9。
  • 那我不知道为什么它抱怨 gems,你可能在 Gemfile.lock 上锁定了一个或另一个特定版本。
  • 但是 Eliot Sykes 已经找到了真正的问题,我没有注意到 respond_with 超出了方法!

标签: ruby-on-rails


【解决方案1】:

尝试将respond_with... 代码移动到destroy 方法中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-17
    • 2013-06-11
    • 2010-12-30
    相关资源
    最近更新 更多