URL中的Rails Params

时间:2013-08-18 21:54:54

标签: ruby-on-rails-3

您好我知道这是常见的问题,但我不能让它工作..我需要从电子邮件链接激活帐户..网址看起来像http://domain.tld/activation/name@domain.tld/d4sa89dsa98,我想从控制器访问该参数: - )

routes.rb

match "activation/:email/:activation_key" => "frontend#activation", :via => :get

frontend_controller.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参数的电子邮件参数,因为它看起来更好,我不确定是否 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]})

1 个答案:

答案 0 :(得分:3)

您是否尝试在constraints: { email: /[^\/]+/ }参数中添加match?可能.被过于贪婪的正则表达式所吞噬。