Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Rails Helper methods

Options
  • 18-05-2011 10:22pm
    #1
    Registered Users Posts: 7,838 ✭✭✭


    Looking to make a helper method in a rails class for users:
    module UsersHelper
    
      def self.full_name
        self.first_name + ' ' + self.last_name
      end
    end
    

    I know thats probably way off but. I can make helpers that take arguments but not the ones that you stick on the end of another object.

    I also tried assigning it to a local variable but I'm not exactly sure what the problem is cause the view never seems to recognise the helpers existence. I'm getting this in the output/view

    undefined method `full_name' for #<User:0x8946724>
    Extracted source (around line #10):
    
    7: 
    8: <p>
    9:   <b>Username:</b>
    10:   <%= @user.full_name %>
    11: </p>
    12: 
    13: <p>
    

    Any help? :)


Comments

  • Registered Users Posts: 11,979 ✭✭✭✭Giblet


    I've never used rails, but can you make the extension method static, or is there a globals file ?


  • Registered Users Posts: 7,838 ✭✭✭Nulty


    Giblet wrote: »
    I've never used rails, but can you make the extension method static, or is there a globals file ?

    eh...I don't know. The helper methods are automatically included. I'm trying to execute the method within the users controller so it really should be recognising it there. I can make this work:
    <p>
      <b>Name:</b>
      <%= full_name(@user.first_name, @user.last_name ) %>
    </p>
    
    module UsersHelper
    
    	  def full_name( first, last)
    		@user.first_name + ' ' + @user.last_name
    	end
        end
    


    This works so I'm just writing the code wrong - I reckon its a "know what your doing" problem! :pac:


  • Registered Users Posts: 7,838 ✭✭✭Nulty


    I put it in the Model....it works now.

    Much learning to be done - long days ahead!


Advertisement