简体   繁体   中英

How to get an Id value from association rails

At this time I need to get and id value from an association Because I create a new note, but i can assign this note to whoever i want, then I have it this way

<%= f.association :user,
        :label      => false,
        :selected   => current_user.id,
        :required   => true,
        :input_html => {
            :class      => 'span4', 
            :disabled   => true, 
            :style      => "float:right", 
            :id         => "usuario"} %>

And the controller create method is this way

 def create
    @note = Note.new(note_params)
    @note.user_id = params[:user]
    render :action => :new unless @note.save
end

But when I press the submit button everything save unless the value for the column :user_id

I have tried with params[:user_id] but it doesn't work

Thanks for your help and sorry for my english

First you need to remove the attribute disabled from your field, a disabled field isn't sended by your form (look at Disabled form fields not submitting data ).

And, your user_id should be placed in something like params[:note][:user_id] , take a look at server log and search for user_id right after you send a POST to server, there be something like:

Started POST "/note" for ::1 at 2013-07-18 15:22:34 +0000
Processing by NoteController#create as */*
  Parameters: {"note"=>{..., "user_id"=>"1", ...}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM