私のユーザーは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
ありがとうございます。私は現在コンピュータから離れていますが、これを少し試してみます。 –
あなたは私に良いオプションをくれました。 –
@KadeWilliamsのお手伝いをしてうれしいです。 –