Ruby on rails 3 rails允许非用户编辑自己的配置文件

Ruby on rails 3 rails允许非用户编辑自己的配置文件,ruby-on-rails-3,devise,cancan,Ruby On Rails 3,Devise,Cancan,我在Rails 3.0中创建了一个安装了Desive&cancan的站点。此应用程序包含4000多人的列表,成员可以查看其中的内容。数据库中很少有人是成员 有没有办法让4000人只更新他们的个人资料,而不必让他们成为用户 我目前的想法是,我将有三个角色(管理员、成员和更新者)。更新程序角色将只能访问其配置文件,仅此而已。我将限制使用cancan 这是最好的方法吗? 我可以随机生成一个密码,并通过电子邮件发送一个链接,在电子邮件中编辑他们的个人资料,当他们点击时,我的程序可以询问他们的登录详细信息

我在Rails 3.0中创建了一个安装了Desive&cancan的站点。此应用程序包含4000多人的列表,成员可以查看其中的内容。数据库中很少有人是成员

有没有办法让4000人只更新他们的个人资料,而不必让他们成为用户

我目前的想法是,我将有三个角色(管理员、成员和更新者)。更新程序角色将只能访问其配置文件,仅此而已。我将限制使用cancan

这是最好的方法吗?
我可以随机生成一个密码,并通过电子邮件发送一个链接,在电子邮件中编辑他们的个人资料,当他们点击时,我的程序可以询问他们的登录详细信息,然后离开。我只是在想,如果不需要生成这么多用户,他们是否是一种更好的方法。

如果你不想让他们成为用户,那么使用令牌系统授予他们临时访问权来完成这项工作

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
链接到“让我更新此配置文件”,标记路径(:id=@profile.id,:method=>:post)
类标记控制器<应用程序控制器
def创建
@profile=profile.find(参数[:id])
如果@profile.send_令牌!
重定向到配置文件路径(@profile),:notice=>“已发送令牌”
其他的
重定向到配置文件路径(@profile),:error=>“无法发送令牌”
结束
结束
结束
类配置文件时间过期。现在
结束
结束
类TokenMailertoken.profile.email,
:subject=>“编辑您的个人资料”,
:body=>“token.key)%>”)
结束
结束
类ProfilesController“令牌已过期”
其他的
@profile=token.profile
结束
当前用户
@profile=current|user.profiles.detect{| p | p.id==params[:id]}
其他的
将\重定向到根\ url,:message=>“不允许”
结束
结束
结束

使用随机生成的密码向您的配置文件模型添加一列,然后创建一个链接来编辑配置文件电子邮件,该密码指向要编辑的文件上的电子邮件地址

class Profile < ActiveRecord::Base
  def generate_edit_key!
    self.edit_key = rand(9999999999)
    save
  end
end

class ProfilesController < ApplicationController
  def request_edit_key
    @profile = Profile.find(params[:id])
    @proifle.generate_edit_key!
    ProfileMailer.edit_profile_request(@profile).deliver
    redirect_to @profile, :notice => 'Mail sent to profile owner'
  end
  def edit
    if current_user
      @profile = current_user.profile
    else
      @profile = Profile.find_by_id_and_edit_key(params[:id], params[:edit_key])
    end
  end
end

class ProfileMailer < ActionMailer::Base
  def edit_profile_request(profile)
    mail(:to => profile.email,
         :subject => 'Edit your profile',
         :body => "<%= link_to 'Click to edit' edit_profile_url(profile, :edit_key => profile.edit_key) %> If you didn't request this, please ignore.")
end
类配置文件“发送给配置文件所有者的邮件”
结束
定义编辑
如果当前用户
@profile=当前用户配置文件
其他的
@profile=profile。通过_id_和_edit_键查找_(参数[:id],参数[:edit_键])
结束
结束
结束
类ProfileMailerprofile.email,
:subject=>“编辑您的个人资料”,
:body=>“profile.edit_key)%>如果您没有请求此操作,请忽略。”)
结束

我必须关闭编辑和更新的设计身份验证检查,有没有一种方法可以在不覆盖用户身份验证的情况下执行此操作!?以下是我当前使用的行:before\u filter:authenticate\u user!,:除了=>[:编辑,:更新]@map7,您可以创建一个特殊的控制器和操作,只用于进行编辑,而不保护该控制器和操作。