lundi 12 juin 2017

My User_id is updated to nil every time I refresh my page in Rails

I know what is causing this issue, but can't find the working alternative of it. I will describe the problem and hope for a good answer.

So, I have a bootstrap modal in which i have a form. that form _for is calling GalleryController#create method.. this is my modal -->

#gallery_create_modal.modal.fade.m{'tabindex':-1 , 'role':'dialog','aria-labelledby':'creategallery'}
    .modal-dialog.modal-lg{'role':'document'}
        .modal-content
            =form_for @gallery , html: {multipart: true}  do |f|

                ...content

                =f.submit 'Upload' , class: 'btn btn-primary'           

and here is my gallery controller

class GalleriesController < ApplicationController

before_action :authenticate_amitian!

def create
@gallery = current_amitian.create_gallery(gallery_params)

end

private

def gallery_params
params.require(:gallery).permit(:g1,:g2,:g3,:g4,:g5,:g6,:amitian_id)
end

end

upto this point if I run the code it returns error saying first element of form can't be empty or nil... means my @gallery is nil

this is because my modal is in my Amitians#show page so to solve this I had to initialize my @gallery inside Amtians#show method like this -->

class AmitiansController < ApplicationController

before_action :authenticate_amitian!, except: [:show]

def index
...content
end

def show
....
@gallery = current_amitian.build_gallery
....

end

This takes away the error but everytime I refresh my page after upload.. it updates my sql table's Gallery.amitian_id to NULL

I can't seem to find a sol. please help. Thank you.

Aucun commentaire:

Enregistrer un commentaire