mardi 15 décembre 2015

Rails project - sub project association

I have a rails app that has several "productions" a user can create. Each production has several lists that the user can create, each corresponding to the current production they are working on. I have a controller for users through Devise, a controller for productions, which has the ability to set the current_userdefault_working_production_id variable. Then I have a controller for crewmembers which is for one of the lists users can generate. I am having trouble associating new items in the 'crewmembers' model with the current working production that the user has set. Additionally, I am having problems displaying only the crewmembers items when they visit the crewmembers index page; currently none are displaying.

From productions_controller.rb

def create
    @production = Production.new(production_params)
    @production.user = current_user
    @production.user_name = current_user.name

    if @production.save
        current_user.default_working_production_id = @production.id
        current_user.default_working_production_name = @production.title
        respond_to do |format|
            if current_user.save
                format.html { redirect_to productions_path } 
            else
                format.html { redirect_to productions_path, notice: "Error.  Please contact support." }
            end
        end
    else
        render 'new'
    end
end

From crewmembers_controller.rb

def index
    @crewmembers = Crewmembers.where(production_id: current_user.default_working_production_id)
end
def create
    @crewmember = Crewmembers.new(crewmember_params)
    @crewmember.production_id = @production_id

    if @crewmember.save
        redirect_to @crewmember
    else
        render 'new'
    end
end

private
    def crewmember_params
        params.require(:crewmember).permit(:firstname, :lastname, :title, :department, :email, :cellphone, :homephone, :streetaddress, :emergencycontactname, :emergencycontactphone, :foodallergies, :medicalnotes, :shirtsize, :notes, :production_id )

    end

On the show page for crewmembers, <%= @crewmember.production_id %> displays nothing. Additionally, the crewmembers index page is blank.

Aucun commentaire:

Enregistrer un commentaire