ruby on rails - NoMethodError in StylesController#new undefined method `styles' for nil:NilClass -


im getting error ruby on rails. made scaffold called style has title , description(string , text), user session ruby gem devise , model called category added style add_category_id_to_styles. changed the

def new  @style = style.new end 

to

def new @style = current_user.styles.build end 

in style controller , giving me undefined method 'styles' nil:nillclass. of know why giving me error?

devise provides method #user_signed_in? check if current user signed in. if yes have access #current_user. can have:

def new   @style = user_signed_in? ? current_user.styles.build : style.new end 

of course can check current_user.present? directly think #user_signed_in? conveys intention better.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -