0

Im trying to pass a variable from controller to my form to no avail.

Its working in another controller but cannot make it work for new user registrations.

  def update
    @profile = Profile.find(params[:id])
    @profile.form = "signup"
    ...
  end

Inside model I can do @profile.form to get the value

This does not work for new user registrations: inside create action:

 undefined method `form=' for nil:NilClass

how to pass a variable to my model upon new user registration? User = User.new then @user.form == "signup" does not work either, i have :form in my attributes accessible list

Part of model:

  attr_accessible form
  ...
  validates :city_id, :presence => true, :if => :signup?


  def signup?
    #@profile.form == "signup"
    #@user.form == "signup"
    if self.form == "signup"
      return true
    else
      return false
    end
  end

EDIT 1: Still unable to pass a param to the model :S:S:S tried every possible way and google found solution.

The create method for my registrations#create =

  def create
    profile = Profile.new
    profile.form = "signup"

    super
  end
4
  • you should also post here the relevant code in models Commented Sep 10, 2012 at 8:02
  • @LukasStejskal updated the question with more info on the models Commented Sep 10, 2012 at 21:04
  • Would you also provide the codes of the views ? Commented Sep 11, 2012 at 17:38
  • @user1022209 posted the create method Commented Sep 12, 2012 at 8:56

2 Answers 2

1

I am not sure what exactly your problem is . What I understand after I read your problem statement twice is that you tried to "new" a instance of Profile, assign its form attribute. And use its instance method signup? to do validation. But you get an error after on self.form because self reference to the nil Class

I think you should change profile to @profile inside the create method. Just by guess after reading ruby guide about instance variable, My thought is that declaring the instance variable that will be added dynamically inside the controller structure and later they are passed to the model. And it is always @... inside controller generated by rails g scaffold.

Just give it a try :)

Sign up to request clarification or add additional context in comments.

2 Comments

def create { @profile = Profile.new; @profile.form =="signup" } then inside the model signup? method i do if @profile.form == "signup" return true and this fails with undefined method `form' for nil:NilClass arghh this is really really frustrating
Try to 。use self.form =="signup" in your method
1

Interesting. I believe what you want to do is have a virtual attribute 'form' of an object a certain value. I.e. does form exist in the database as well. If it doesn't you should be using attr_accessor :form Documentation

This defines the getter and setter method for form in the class where it is being invoked.

However, the error you stated is something radically different.

undefined method `form=' for nil:NilClass

This merely means a profile with the id being passed in the params does not exist. i.e. if we are updating params id = 222, a database record with id 222 does not exist for Profile.

3 Comments

in profilescontroller#update it works, there i set @profile.form = "signup" but on user registration there is only create action where this can be set, so im still in the dark on this
Please share your create method. I apologize if I was unclear earlier, however what I am trying to say is that the variable on which you are trying to do .form = is not a reference to a Profile object. It is nil
:Ive posted the create method hope that clears things up a bit

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.