我的用戶是通過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。 –