jeudi 16 février 2017

going to new page sets my user_id to null in Rails

I have 2 models .. one for User(amitian) and other about They have a has_one and belongs_to association Problem is that whenever I create a new about it works fine and set my amitian_id to current_amitian but whenever I go to my new page again.. it updates my amitian_id to NULL

for eg. this is my sql after I submit the form insert into about_amitian('values' , amitian_id = 1) and if i go to new page again it says update about_amitian set amitian_id = null where about_amitian.id= 1

this is my controller and model

class AboutAmitiansController < ApplicationController

  before_action :authenticate_amitian!

  def new
    @amitian = current_amitian
  @about_amitian = @amitian.build_about_amitian
  end

  def create
    @amitian = current_amitian
    @about_amitian = @amitian.create_about_amitian(about_amitian_params)
    if @about_amitian.save
            redirect_to root_url
            flash[:notice] = 'success'
        else
            render root_url
        end
  end


private

    def about_amitian_params
    params.require(:about_amitian).permit(:dob,:interest,:bio,:catch_phrase,:relationship_status)
    end

end

and model

belongs_to :amitian 

and in amitian has_one :about_amitian

Why is it Updating my database ?

Aucun commentaire:

Enregistrer un commentaire