2017-10-08 4 views
2

私のユーザーはDeviseからセットアップされています。 CanCanCanも使用できます。記事が自分のものであれば、編集と削除のリンクしか表示されないようにする方法は?

私は記事モデルを設定し、どのユーザーも記事を作成できます。自分の記事作成のみを削除したり編集したりすることができます。索引では、すべてのユーザーが作成したすべての記事を表示できます。現在、表示、編集、および削除のオプションがあります。私はそのオプションが、ユーザーが所有している記事に表示されるようにしたいだけです。私は他のすべての記事行を空白にします。 (もちろん、管理者を除く。) ユーザービュー/記事の投稿を閲覧することができます/ index.html.erb

<table> 
    <tr> 
    <th>Title</th> 
    <th>Description</th> 
    </tr> 

    <% @articles.each do |article| %> 
    <tr> 
     <td><%= article.title %></td> 
     <td><%= article.description %></td> 
     <td><%= link_to 'View', article_path(article) %></td> 
     <td><%= link_to 'Edit', edit_article_path(article) %></td> 
     <td><%= link_to 'Delete', article_path(article), 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %></td> 
    </tr> 
    <% end %> 
</table> 

どのように私は、ユーザーが唯一の編集を参照して、自分が所有ポスト上のボタンを削除できるようにすることができますか?

私はこれを試してみましたが、それは動作しません:

<table> 
    <tr> 
    <th>Title</th> 
    <th>Description</th> 
    </tr> 

    <% @articles.each do |article| %> 
    <tr> 
     <td><%= article.title %></td> 
     <td><%= article.description %></td> 
     <td><%= link_to 'View', article_path(article) %></td> 
     <% if user_signed_in? && current_user.articles.exists?(@article.id) %> 
     <td><%= link_to 'Edit', edit_article_path(article) %></td> 
     <td><%= link_to 'Delete', article_path(article), 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %></td> 
     <% end %> 
    </tr> 
    <% end %> 
</table> 

私も試してみた:ここ

​​

は私の記事のコントローラは次のようになります。(私はそれをする必要がある知っていますよく見える。)

def create 
    @article = current_user.articles.build(article_params) 

    if @article.save 
    redirect_to @article 
    else 
    render 'new' 
    end 
end 


def update 
    @article = Article.find(params[:id]) 
    if user_signed_in? && current_user.articles.exists?(@article.id) 
    if @article.update(article_params) 
     redirect_to @article 
    else 
    render 'edit' 
    end 
    elsif current_user && current_user.admin_role? 
    if @article.update(article_params) 
     redirect_to @article 
    else 
    render 'edit' 
    end 
    else 
    redirect_to @article 
    end 
end 

def destroy 
    @article = Article.find(params[:id]) 
    if user_signed_in? && current_user.articles.exists?(@article.id) 
    @article.destroy 
    redirect_to articles_path 
    elsif current_user && current_user.admin_role? 
    @article.destroy 
    redirect_to articles_path 
    else 
    redirect_to articles_path 
    end 
end 

答えて

1

をあなたが工夫によって提供さcurrent_userヘルパーへのアクセス権を持っているとして、あなたは、COMができ記事の所有者と引き離してください。これは、アクションを実行するために、適切なリンクをレンダリングするために、ビューになります

<% @articles.each do |article| %> 
    <tr> 
    <td><%= article.title %></td> 
    <td><%= article.description %></td> 
    <td><%= link_to 'View', article_path(article) %></td> 
    <% if current_user == article.user %> 
     <td><%= link_to 'Edit', edit_article_path(article) %></td> 
     <td><%= link_to 'Delete', article_path(article), 
       method: :delete, 
       data: { confirm: 'Are you sure?' } %></td> 
    <% end %> 
    </tr> 
<% end %> 

あなたが好き、あなたの意見のほとんどで利用可能にApplicationHelperで、ヘルパーにこの検証を移動することができます。

module ApplicationHelper 
    def owner?(object) 
    current_user == object.user 
    end 
end 

オブジェクトを渡すと、現在のユーザーがobjectユーザーと等しいかどうかによってtrueまたはfalseが返されます。あなたは、コントローラにこの検証を移動したい、あるいはまた、あなたは両方のケースでそれを使用したい場合は、あなたがコントローラで同じ検証を行うことができる場合

<% if owner?(article) %> 

:ビューにのみに変更されます。あなたはbeforeコールバックにこの部分を移動したい場合は、

def edit 
    unless current_user == @article.user 
    redirect_back fallback_location: root_path, notice: 'User is not owner' 
    end 
end 

owner?ヘルパーメソッドは、コントローラでは利用できないので、あなただけのように、CURRENT_USERは、物品の所有者でない場合には戻ってリダイレクトすることができます

private 

def owner? 
    unless current_user == @article.user 
    redirect_back fallback_location: root_path, notice: 'User is not owner' 
    end 
end 

この方法であなたを:あなたはプライベートメソッドとして追加することができ、あなたがこのような比較を行うことができます@articleへのアクセス権を持つ、それはなるだろう、editdestroy方法でそれを使用することができるようにしますbefore_actionコールバックとして追加する必要があります:

before_action :owner?, only: %i[edit destroy] 

おそらく@article変数を定義する前のものです。

Rails 5でredirect_backを使用していることに注意してください。以前のバージョンではredirect_to :backが使用されている可能性があります。

+0

ありがとうございます。私は現在コンピュータから離れていますが、これを少し試してみます。 –

+0

あなたは私に良いオプションをくれました。 –

+0

@KadeWilliamsのお手伝いをしてうれしいです。 –

関連する問題