ruby on rails - simple_form association doesn't save belongs_to id -


i have recipient , category model. it's simple association of 1 category has many recipients. when try update recipient form , assign category, won't save record. if use console , update record manually, e.g. recipient.update(9, category_id: 13), see correct category assigned recipient when try edit/update record, won't save new chosen category.

here recipient model

class recipient < activerecord::base   belongs_to :category    accepts_nested_attributes_for :category end 

here category model

class category < activerecord::base   has_many :recipients   validates :category, presence: true    default_scope { order('category')} end 

here recipient controller

class recipientscontroller < applicationcontroller   def index     @recipients = recipient.order(:recipient_name).page(params[:page])   end    def new     @recipient = recipient.new   end    def show     @recipient = recipient.find(params[:id])   end    def create     @recipient = recipient.new(recipient_params)     if @recipient.save       redirect_to recipients_path     else       render :new     end   end    def edit     @recipient = recipient.find(params[:id])   end    def update     @recipient = recipient.find(params[:id])      recipient_params = params.require(:recipient).permit(:recipient_name, :alternate_name, :description, :city, :state, :country, category_attributes: [:category, :id])     @recipient.update_attributes(recipient_params)      redirect_to recipient_path(id: @recipient.id)   end    def destroy     @recipient = recipient.find(params[:id])     @recipient.destroy      redirect_to recipients_path   end    private   def recipient_params     params.require(:recipient).permit(:recipient_name, :alternate_name, :description, :city, :state, :country, product_attributes: [:product_name, recipient_products: [:recipient_id, :product_id]], channel_attributes: [:channel_name, recipient_channels: [:recipient_id, :channel_id]],  category_attributes: [:id, :category])   end  end 

here edit view

<div class="row">   <div class="col-sm-6">     <h2>edit <%= @recipient.recipient_name %></h2>      <%= simple_form_for @recipient |form| %>       <%= form.error_notification %>       <%= form.input :recipient_name, placeholder: 'recipient', label: 'recipient name' %>       <%= form.input :alternate_name, placeholder: 'alternate name' %>       <%= form.association :category, label_method: :category, value_method: :id %>       <%= form.input :description, placeholder: 'description'%>       <%= form.input :city, placeholder: 'city'%>       <%= form.input :state, placeholder: 'state' %>       <%= form.input :country, as: :country, priority: ['us', 'ca'] %>       <%= form.button :submit, 'update recipient', {:class=>"btn btn-secondary"} %>       <%= link_to "cancel", :back, {:class=>"btn btn-default"} %>     <% end %>   </div> </div> 

and here routes.rb file

rails.application.routes.draw   root to: 'home#index'   resources :media_points   resources :products   resources :channels   resources :recipients   resources :media_point_products   resources :distributions   resources :categories     resources :recipients   end   '/listing' => "listing#index"   devise_for :admins   devise_for :users   resources :users end 

i wrote answer since format pain in comments:

change recipient_params private method from: category_attributes: [:category, :id]

to

 category_id: param_that_has_category_id_here 

also, have 2 routes recipients , going utilize first matching route not nested recipients in categories route have further down. if first change in private method didn't fix specify nested situation in simpleform doing follows since looking use nested route:

  simple_form_for [@category, @recipient], url: 'nested_route_path_here' |f| 

just add @category = category.new new action in recipients controller , in create action you'll have category param sent via params[:category_id]