您好,我知道这是常见问题,但我不能让它工作..我需要通过电子邮件链接激活帐户.. url 看起来像http://domain.tld/activation/name@domain.tld/d4sa89dsa98
我想从控制器访问该参数:-)
路线.rb
match "activation/:email/:activation_key" => "frontend#activation", :via => :get
前端控制器.rb
def activation
@user = User.find(:conditions => {:email => params[:email], activation_key => params[:activation_key]})
render "activation"
end
我得到错误
No route matches [GET] "/activation/name@email.tld/a46d4sa8dsa68d"
我的activation.html.erb
<% if @user %>
Activation successful.
<% else %>
Activation key is invalid.
<% end %>
编辑: - - - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -------------------------
我用 :id 参数替换了 :email 参数,因为它看起来更好,我不确定是否
Array.new(32){Random.new.rand(36).to_s(36)}.join
是唯一字符串,但现在没关系。
路线
match "activation/:id/:activation_key" => "frontend#activation", :via => :get
耙路线
GET /activation/:id/:activation_key(.:format) frontend#activation
前端控制器
def activation
@user = User.find(:conditions => {:id => params[:id], :activation_key => params[:activation_key]})
render "activation"
end
错误
Couldn't find User without an ID
但这有效,所以问题出在
@user = User.find(params[:id])
解决方案
@user = User.find(:first, :conditions => {:id => params[:id], :activation_key => params[:activation_key]})