开发者

How can I bind a Rails 3 route parameter to a nested value in the params hash?

I would like to have a route like this

get "users/sms_confirmation/:sms_confirmation_code" => "users#sms_confirmation"

bind the :sms_confirmation_code param like the following example:

A request to users/sms_confirmation/ABC123 results in the params hash containing

{:user => {:sms_confirmation_code => "ABC123"}} # This is what I want

Currently the route gives me:

{:sms_confirmation_code => "ABC123"} # This is what I've got.

As of yet I cannot find a way to do this with Rails 3 routing. Is there a way to accomplish this?

Working around this problem i开发者_如何学JAVAs easy, but I don't want to do it if it's unnecessary.

UPDATE: sms_confirmation_code is a fields in the users table. My goal is to use pretty URLs such as the one in the example above rather than a query string appended to the URL. I also want to avoid extra code in the controller to structure a hash like described above.


sms_confirmation is nested under users right?

shouldn't it be something likeusers/123/sms_confirmation/ABC123 ?

resources :users do
  resources :sms_confirmation do
    get "users/:user_id/sms_confirmation/:sms_confirmation_code" => "users#sms_confirmation"
  end
end

if you don't have a users controller you could use a namespace such as

namespace "users" do
 resources :sms_confirmation do
  get "sms_confirmation" => "users#sms_confirmation"
 end     
end

Tell me if it worked!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜