Loading of views and controller actions

Hey all,

Let’s say you want a customized route. You want to create a helper for
it (e.g. password_recovery_root_path) and a url path (e.g.
passwords/recovery) that when it is invoked, points the user to the
passwords controller’s get_email method. So you would do this:

map.resources :passwords, :only => %w(new create), :collection =>
{:check => :post}

map.with_options :name_prefix => “password_recovery_”, :path_prefix =>
“passwords/recovery”, :controller => “passwords” do |password_recovery|
password_recovery.root :action => “get_email”
… (more code)
end

Now when the get_email method is invoked, it checks if the email key
contains a value in query string, and if so, it links user somewhere
else. Otherwise it renders a view email.html.haml, which is exactly the
same page that got loaded when the get_email method was called first:

def get_email
if params[:email].present?
@user = User.find_by_email!(params[:email])
redirect_to password_recovery_show_questions_path(:email =>
params[:email])
else
render “password_recovery/email”
end

end

The view in question is located in password_recovery/email.html.haml.

When you look at server logs, you can see this:

Started GET “/passwords/recovery”
Processing by PasswordsController#get_email as HTML
Rendered password_recovery/email.html.haml

My question is when that get_email method was called, we never specified
to render password_recovery/email.html.haml . The only time when that is
specified is when the user clicks the submit button and a get request is
sent to server and if nothing passed in email query string, then the
page simply reloads via render “password_recovery/email”. However, the
second time it reloads, there are some additional parameters in the
query string: /passwords/recovery?utf8=%E2%9C%93&email=&commit=Continue.
Since what the get_email method is doing is clear as day, I am not sure

  1. how rails knows to load the specific email view when the page is
    first routed, as shown above 2) and where those additional parameters
    came from that were passed into query string the second time page load
    (if no email was provided).

Thanks for response.

On May 15, 8:29pm, John M. [email protected] wrote:

first routed, as shown above 2) and where those additional parameters
came from that were passed into query string the second time page load
(if no email was provided).

Not sure what you are confused about - first time round there is no
email parameter, so you end up in the else clause. The second time
round the parameters presumably come from the form that was rendered
in response to the the first request, but this is your application not
mine so you’re the best judge of what it’s doing.

Fred

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs