主题中的ArgumentError显示可能的设计错误?

时间:2016-03-29 21:33:18

标签: ruby-on-rails ruby devise

我在这里有一些新手,需要一些帮助来解决这个问题。我每次尝试创建或编辑新的"主题时都会抛出一个参数错误。"

ArgumentError

以下是" show:"

的代码



<h1><%= @topic.title %></h1>
<%= link_to "Edit", edit_topic_path(@topic), class: 'btn btn-success' %>
<%= link_to "Delete Topic", @topic, method: :delete, class: 'btn btn-danger', data: {confirm: 'Are you sure you want to delete this topic?'} %>
<% if policy(Bookmark.new).create? %>
  <%= link_to "New Bookmark", new_topic_bookmark_path(@topic), class: 'btn btn-success' %>
<% end %>
<% @topic.bookmarks.each do |bookmark| %>
  <div class="media-body">
    <div class="row">
      <div class="col-md-2">
        <div class="container">
          <img src="http://icons.better-idea.org/icon?url=<%= bookmark.url %>&size=120">
          <div class="media-heading">
            <%= link_to bookmark.name, topic_bookmark_path(@topic, bookmark) %>
          </div>
        </div>
      </div>
    </div>
    <div class="col-md-1">
      <%= render partial: 'likes/like', locals: {bookmark: bookmark} %>
    </div>
  </div>
<% end %>
&#13;
&#13;
&#13;

这是&#34;主题&#34;控制器:

class TopicsController < ApplicationController
  def index
    @topics = Topic.all
  end

  def show
    @topic = Topic.find(params[:id])
  end

  def new
    @topic = Topic.new
  end

  def create
    @topic = Topic.new(topic_params)

    if @topic.save
      flash[:notice]= "Topic was saved."
      redirect_to @topic
    else
      flash.now[:alert]= "The topic could not be saved. Please try again"
      render :new
    end
  end

  def edit
    @topic = Topic.find(params[:id])
  end

  def update
    @topic = Topic.find(params[:id])
    @topic.assign_attributes(topic_params)

    if @topic.save
      flash[:notice]= "The topic was saved sucessfully."
      redirect_to @topic
    else
      flash.now[:alert]= "There was an error saving the topic. Please try again."
      render :edit
    end
  end

  def destroy
    @topic = Topic.find(params[:id])

    if @topic.destroy
      flash[:notice]= "\"#{@topic.title}\" was deleted successfully."
      redirect_to topics_path
    else
      flash.now[:alert] = "There was an error in deleting this topic."
      render :show
    end
  end

  def topic_params
    params.require(:topic).permit(:title)
  end
end

更新(1):删除&#34;策略&#34;后出现新错误New Error

以下是使用Pundit的&#34;应用程序策略:

class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    raise Pundit::NotAuthorizedError, "must be logged in" unless user
    @user = user
    @record = record
  end

  def index?
    false
  end

  def show?
    scope.where(:id => record.id).exists?
  end

  def create?
    user.present?
  end

  def new?
    create?
  end

  def update?
    user.present? && ( record.user == user )
  end

  def edit?
    update?
  end

  def destroy?
    user.present? && (record.user == user)
  end

  def scope
    Pundit.policy_scope!(user, record.class)
  end

  class Scope
    attr_reader :user, :scope

    def initialize(user, scope)
      @user = user
      @scope = scope
    end

    def resolve
      scope
    end
  end
end

3 个答案:

答案 0 :(得分:1)

问题在于使用Pundit gem制定的策略。你可以检查一下名为BookmarkPolicy的东西或类似的东西,或者至少在这里发布。你忘记了控制器中的include Pundit了吗?

答案 1 :(得分:1)

您正在使用Pundit gem,但我没有在控制器中看到授权方法。来自Pundit's documentation

  

假设你有一个Post类的实例,Pundit现在可以让你   在您的控制器中执行此操作:

def update
  @post = Post.find(params[:id])
  authorize @post
  if @post.update(post_params)
    redirect_to @post
  else
    render :edit
  end
end
  

授权方法自动推断Post会有一个   匹配PostPolicy类,并实例化此类,交付   当前用户和给定记录。

答案 2 :(得分:0)

屏幕截图中的源代码和您发布的代码不一样。你的代码:

<% if policy(Bookmark.new).create? %>

屏幕截图:

<% if (Bookmark.new).create? %>

Rails正确报告Bookmark.new没有create?方法,因为它缺少policy()方法调用。

您的文件已保存吗?你确定要更改正确的文件吗?