如果你不想讓他們成爲用戶,那麼使用令牌系統來授予他們臨時訪問權限來執行此操作。
link_to 'Let me update this profile', token_path(:id = @profile.id, :method => :post)
class TokensController < ApplicationController
def create
@profile = Profile.find(params[:id])
if @profile.send_token!
redirect_to profile_path(@profile), :notice => 'A token has been sent'
else
redirect_to profile_path(@profile), :error => 'A token could not be sent'
end
end
end
class Profile < ActiveRecord::Base
has_many :tokens
def send_token!
token = self.tokens.create()
TokenMailer.update_profile(token).deliver
end
end
class Token < ActiveRecord::Base
belongs_to :profile
before_save :generate_key, set_expiration
def generate_key
self.key = rand(999999999999)
end
def set_expiration
self.expires_at = 1.day.from_now
end
def expired?
expires_at > Time.now
end
end
class TokenMailer < ActionMailer::Base
def update_profile(token)
mail(:to => token.profile.email,
:subject => 'Edit your profile',
:body => "<%= link_to 'Click to edit' edit_profile_url(token.profile, :key => token.key) %>")
end
end
class ProfilesController < ApplicationController
def edit
if params[:key]
token = Token.find_by_key(params[:key])
if token.expired?
redirect_to root_url, :message => 'Token Expired'
else
@profile = token.profile
end
elsif current_user
@profile = current_user.profiles.detect{|p| p.id == params[:id] }
else
redirect_to root_url, :message => 'Not allowed'
end
end
end
我不得不關閉設計認證檢查編輯和更新,有沒有辦法解決這個問題,而不需要重寫authenticate_user!?這裏是我目前使用的行: before_filter:authenticate_user !,:except => [:edit,:update] – map7
@ map7你可以創建一個特殊的控制器和動作來執行編輯,而不是保護那個。 – Unixmonkey