Friday, 15 May 2015

ruby - Using devise current_user -



ruby - Using devise current_user -

must having bad day troubling me , cant seem figure out why

i have user , address illustration , relation follows

class user < activerecord::base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable has_one :address end class address< activerecord::base belongs_to :user end

now when create address object want able pass through current_user id , save address model, in controller have

before_filter :authenticate_user! def new @address= current_user.address.new end def create @address= current_user.address.new(address_params) if @address.save redirect_to root_path, notice: 'address created' else render action: 'new' end end private def address_params params.require(:address).permit(:id, :user_id, :add_1, :add_2, :add_3) end

i cant access new action @ moment error getting

undefined method `new' nil:nilclass

what missing blindly obvious, head not thinking straight today

any pointers appreciated

thanks

i think should this:

def new @address = current_user.build_address end

and same in create action

more info here

ruby ruby-on-rails-4 devise associations

No comments:

Post a Comment