开发者

rails3: got something to work by adding 'super' but no idea why, can anyone explain?

开发者 https://www.devze.com 2023-03-19 04:30 出处:网络
Still a rails and ruby newbie, and I\'m trying to figure out why adding \'super\' fixed a problem I was having. (Was actually just a dumb guess to try \'super\' so I have zero understanding why it now

Still a rails and ruby newbie, and I'm trying to figure out why adding 'super' fixed a problem I was having. (Was actually just a dumb guess to try 'super' so I have zero understanding why it now works.)

My rails 3 app uses Devise, and needed to "stuff" certain fields on the registration form if they are specified on the URL...

For example my normal registration url is

http://localhost:3000/users/register

which displays a blank signup form.

What I'm doing is having the url

http://localhost:3000/users/register?referral_code=ABCDEF

display the registration form, but with the referral_code field already filled in.

I already had a custom devise registration controller working (because I needed to redirect to a custom "check your email" page after a new user signs up before they have clicked their email confirm link). My routes.rb is therefore modified to:

devise_for :users, :path_names => { :sign_up => "register"}, 
:controllers => { :registrations => "registrations" }

and my simple custom devise registrations controller currently looks like:

class RegistrationsController < Devise::RegistrationsController 
  def new
    unless params[:refer].nil?
      @initial_referred_by = params[:refer].upcase
    end
    super
  end

  protected   

  def after_inactive_sign_up_path_for(resource)
     "/setup_awaiting_confirmation"
  end
end

Before I added 'super' to my custom 'new' method, the view would throw an error about missing 'error' method on the line of the view code that displa开发者_StackOverflow社区yed devise_error_messages!

On one hand, I'm glad it works now. But I'd really like to have some understanding of what it actually happening here -- why 'super' was ne cessary and what it's doing -- as I try to come up to speed on ruby+rails.

I wrote lots of other controller methods and never had ot use it, so I think it has something to do with my method needing to 'call' the underlying (or over-lying?) devise method also?


super keyword is inserting code from original implementation of method when it's re-defined (in this case, it inserts Devise::RegistrationsController.new method). It was probaly needed because required handling of things in view requires that original code. If something's still unclear, please ask.

0

精彩评论

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

关注公众号