2016-03-29 16 views
0

私はここにレールニューブラーのビットがあり、これを理解する助けが必要です。私は新しい「トピック」を作成したり編集したりするたびにスローされる引数エラーがあります。トピックのArgumentError#可能なDeviseエラーを表示しますか?

ArgumentError

ここで "ショー" のためのコードである:

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ここ

<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 %>

は "トピック" コントローラであります): "ポリシー" 01を削除した後の新しいエラーここで

は評論家を使用する「アプリケーションポリシーです:

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 
+0

あなたは、取得しているエラーを投稿することができます – MZaragoza

+0

引数エラーは、画像へのリンクです。 http://i.stack.imgur.com/jN7Gj.png –

+0

ポリシーなしで試しましたか? ( 'link_to"のみ新しいブックマーク "..') – BriceB

答えて

1

問題は、評論家の宝石で作られたポリシーです。 BookmarkPolicyなどと呼ばれるものをチェックしたり、少なくともここに投稿してください。あなたのコントローラのinclude Punditを忘れましたか?

+0

私はその本当の速さを試してみましょう... –

+0

インクルードを追加するPunditエラーを助けません。ブックマークポリシーで元のコードを更新します。 –

+0

元の投稿をアプリケーションポリシーで更新しました。ブックマークに固有のポリシーはありません。アプリケーションコントローラには、Pundit –

1

Pundit gemを使用していますが、コントローラにauthorizeメソッドが表示されません。 Pundit's documentationから:

あなたはクラスのポストのインスタンスを持っていると仮定すると、評論家は今、あなたのコントローラでこれを行う することができます:

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

のauthorizeメソッドは自動的にポストが持っていることを推測しますa PostPolicyクラスに一致し、このクラスをインスタンス化して、 現在のユーザーと指定したレコードを渡します。

0

あなたが投稿したスクリーンショットのソースコードとコードは同じではありません。あなたのコード:

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

スクリーンショット:それはpolicy()メソッド呼び出しが欠けているため

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

Railsは正しく、Bookmark.newcreate?メソッドを持っていないことを報告しています。

ファイルは保存されていますか?正しいファイルを変更してもよろしいですか?

関連する問題