認可ビュー要素を示していないカンカン
-
26-09-2019 - |
質問
私はRailsのとカンカン工夫/といくつかの基本的な認証/認可を取得しようとしています。
:むしろ基本的な何かをしようとしているライアン・Bのスクリーンキャストのような役割や私の周りの他の例を使用するよりも 1 - ユーザーが
にログインすることができます
2 - ユーザーがのみ編集/自分の記事破壊することができます(なしの役割を、あなたは、のいずれかでログインしていると、新しい記事や編集を作成することができます/自分自身を破壊したり、あなたがログアウトしていると、あなただけの記事、ログインを見ることができます)
私は、最初の部分のための工夫を使用していますし、それがうまく働いていますが、私はカンカンでの作業第二部を得ることができません。編集してログインしていると、直接URL(例えば/記事/ 3 /編集)まだ記事が別のユーザーのためであっても、可能にしたときに表示されません記事へのリンクを破壊します。
私ability.rb
がある
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user
if user.nil?
can :read, :all
else
# can :manage, :all #test - with this, all the edit/destroy links appear
can :manage, Article, :user_id == user
end
end
end
articles_controller.rb
ます:
class ArticlesController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show] # for Devise
load_and_authorize_resource
# GET /articles
# GET /articles.xml
def index
@articles = Article.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
# GET /articles/1
# GET /articles/1.xml
def show
@article = Article.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/new
# GET /articles/new.xml
def new
@article = Article.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
end
# POST /articles
# POST /articles.xml
def create
@article = Article.new(params[:article])
@article.user = current_user
respond_to do |format|
if @article.save
format.html { redirect_to(articles_path, :notice => 'Article was successfully created.') }
format.xml { render :xml => articles_path, :status => :created, :location => articles_path }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.xml
def update
@article = Article.find(params[:id])
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.xml
def destroy
@article = Article.find(params[:id])
@article.destroy
respond_to do |format|
format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
end
とリストの記事が_article_list.html.erb
という部分ビューます:
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= article.user_id %></td>
<td><%= link_to 'Show', article %></td>
<% if can? :update, @article %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<% end %>
<% if can? :destroy, @article %>
<td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
<% end%>
</tr>
<% end %>
</table>
この設定で、編集/ブランケットcan :manage, :all
がありますしない限り、表示されませんビュー内のリンクを破壊し、でもcan :manage, Article
は動作しません。私は前述したように、それはまた、まっすぐに記事を編集するにディープリンクをしていることが、あなたのように実際の行動を制限されていないと、それはそれを許可します。
私は私が間違ってここをやっているかわからないんだけど。いくつかの助けを得るために素晴らしいことです。
事前
に感謝
ジェイソン
解決
私は私の問題を解決するために管理しました。 I私の環境をリセット(RVM - 宝石やgemsetsをresintalled - ルビー1.9.2と3.0.0をレール)とのコードの一部を変更し、私が持っていたすべての問題は、(リダイレクトループ去っていきました、ビュー要素は、ログに記録されているに基づいて変更していませんで、まだpermissable不正コントローラアクション)。私はability.rb
、articles_controller.rb
、および_article_list.html.erb
を貼り付けました。
ability.rb
ます:
class Ability
include CanCan::Ability
def initialize(user)
if user
can :create, Article
can :read, :all
can :update, Article, :user_id => user.id
can :delete, Article, :user_id => user.id
else
can :read, :all
end
end
end
私はそれが今で理にかなっていると思いますが、唯一の更新や削除は、現在のユーザーの記事をすることになったので、私は具体的にはCRUD要素を分割ます。
articles_controller.rb
class ArticlesController < ApplicationController
before_filter :authenticate_user!, :except => [:index, :show]
# load_and_authorize_resource # RESTful automated CanCam authorization - excludes non RESTful
# GET /articles
# GET /articles.xml
def index
@articles = Article.all
authorize! :read, @articles
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @articles }
end
end
# GET /articles/1
# GET /articles/1.xml
def show
@article = Article.find(params[:id])
authorize! :read, @article
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/new
# GET /articles/new.xml
def new
@article = Article.new
authorize! :create, @article
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @article }
end
end
# GET /articles/1/edit
def edit
@article = Article.find(params[:id])
authorize! :update, @article
end
# POST /articles
# POST /articles.xml
def create
@article = Article.new(params[:article])
@article.user = current_user
authorize! :create, @article
respond_to do |format|
if @article.save
format.html { redirect_to(articles_path, :notice => 'Article was successfully created.') }
format.xml { render :xml => articles_path, :status => :created, :location => articles_path }
else
format.html { render :action => "new" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# PUT /articles/1
# PUT /articles/1.xml
def update
@article = Article.find(params[:id])
authorize! :update, @article
respond_to do |format|
if @article.update_attributes(params[:article])
format.html { redirect_to(@article, :notice => 'Article was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @article.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /articles/1
# DELETE /articles/1.xml
def destroy
@article = Article.find(params[:id])
@article.destroy
authorize! :delete, @article
respond_to do |format|
format.html { redirect_to(articles_url) }
format.xml { head :ok }
end
end
def by
@user = User.find(params[:id])
@articles = @user.articles
authorize! :read, @articles
end
end
load_and_authorize_resource
は動作しますが、私は特定のAUTHORIZEを入れてきました!各コントローラのアクションの行私は底に余分なアクションを持っているよう。どちらも今の仕事ます。
私は_article_list.html.rb
で、リスト内の現在の記事を参照するために記事を@articleへの参照を更新します:
<table>
<tr>
<th>Title</th>
<th>Description</th>
<th>User</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.description %></td>
<td><%= article.user_id %></td>
<td><%= link_to 'Show', article %></td>
<% if can? :update, article %>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<% end %>
<% if can? :delete, article %>
<td><%= link_to 'Destroy', article, :confirm => 'Are you sure?', :method => :delete %></td>
<% end %>
</tr>
<% end %>
</table>
すべてが今取り組んでいます。ここでうまくいけば、助けてくれてありがとう、この意志のヘルプ誰かアウト彼らはこの問題に実行する場合。
他のヒント
は、ユーザIDをマッチングするためのあなたの条件はかなり右ではありません。それはする必要があります:
can :manage, Article, :user_id => user.id
のチェックをしたい属性を使用すると、に対してチェックする値にマップされます。
それはあなたがそれを初期化しましたので、nilをすることができない場合も、あなたはuser.nil?
のためにチェックしています。 (物事の多くを試したことのおそらく症状!)
あなたのキャッチの仕事をしていますか?あなたが缶のコメントを解除した場合:、管理:すべての行は、ユーザーが(誰ももちろん、他のと一緒に)彼/彼女の投稿を編集することができます。
? へのuser_id ==ユーザー:あなたは、変更しようとしたことがあり、することができます、管理の記事、
can :manage, Article do |article|
article.try(:user) == user
私は何か間違っをしていたと思われるが、ワーク・するには、authorizeをロード得ることができたことはありません。直接URLにアクセスされるのを防ぐために、あなたの記事の編集アクションで、この
を追加してみてください unauthorized! if cannot? :edit, @article