nested routes not rendering properly in link helper

4 views
Skip to first unread message

John Merlino

unread,
Oct 30, 2011, 9:31:50 PM10/30/11
to Ruby on Rails: Talk
hey all,

I have this:


scope :path => '/activation', :controller => :activation do
post "create" => :create
get "confirmation" => :confirmation, :as => "confirmation"
get "send_activation_notification"
=> :send_activation_notification, :as => "send_activation" do
resources :users do
resources :accounts
end
end
end


After an activation is created:

def create
if acc_a_usr = @activation.perform
extract_contents acc_a_usr
send_activation_notification @user, @account
render :action => "confirmation"
else
render "new"
end
end

confirmation view loads:

= link_to "Resend Email", send_activation_path(@user, @account)


That link gets directed to this method (this method gets called in
multiple places so must account for multiple situations):
def send_activation_notification(user=nil, account=nil)
user = user || params[:user]
account = account || params[:account]
CreateAccount.create_account(user, account).deliver
end


Now I look at the url generated by this helper and it's not right:

send_activation_notification.33?=59

it should be:
send_activation_notification/user/33/account/59 (so that I can access
these ids in the params hash)

I have rea the book "The Rails 3" way and their example of doing what
I want is somehting like this:

resources :actions do
resources :bids
end

In my code I try to mimic something similar:

get "send_activation_notification"
=> :send_activation_notification, :as => "send_activation" do
resources :users do
resources :accounts
end
end

But it doesn't work for me. Note that send_activation_notification is
not a restful route, so I couldnt model my code exactly as shown in
the rails book.

thanks for response

John Merlino

unread,
Oct 31, 2011, 11:33:20 AM10/31/11
to Ruby on Rails: Talk
This was easy resolution:

send_activation_notification(:user_id => @user.id, :account_id =>
@account.id)

That will pass the params in query string

Tim Shaffer

unread,
Oct 31, 2011, 3:26:26 PM10/31/11
to rubyonra...@googlegroups.com
On Sunday, October 30, 2011 9:31:50 PM UTC-4, John Merlino wrote:

   get "send_activation_notification"
=> :send_activation_notification, :as => "send_activation" do
   resources :users do
               resources :accounts
             end
 end

But it doesn't work for me. Note that send_activation_notification is
not a restful route, so I couldnt model my code exactly as shown in
the rails book. 

That's not a valid route at all. I think you might have it backwards. Check out the results of "rake routes" if you need to see what all your routes look like.

Instead of this:

get "send_activation_notification" => :send_activation_notification, :as => "send_activation" do 
  resources :users do 
    resources :accounts 
   end 
 end 


You might want to do something like this:

resources :users do
  get 'send_activation_notification', :on => :member
  resources :accounts
end

Then you will have a send_activation_notification_user route that you can use.
 

John Merlino

unread,
Nov 1, 2011, 7:20:41 PM11/1/11
to Ruby on Rails: Talk
thanks for response, the solution I provided also works if you want to
pass params in hash via query string when there isn't a direct
association between multiple models
Reply all
Reply to author
Forward
0 new messages