ga-wdi-exercises / project2

[project]
1 stars 30 forks source link

Issue with merging user hash to tie to creation of a resource (using Devise) #880

Closed wjpurdum closed 7 years ago

wjpurdum commented 7 years ago

I'm trying to authenticate my program creations through merging user hashes with program params. My "favorites" are registering, as they are showing up for the user, but I'm getting this error that user isn't recognized when I try to tie them to the creation of a program:

This seems to recognize the user:

def add_favorite
  @program = Program.find(params[:program_id])
  @program.favorites.create(user: current_user)
  @favorite = Favorite.find_by(program: @program, user: current_user)
  redirect_to :back

  end

This does not:

def create
  @state = State.find(params[:state_id])
  @station = Station.find(params[:station_id])
  @program = @station.programs.create!(program_params.merge(user: current_user))
  redirect_to state_path(@state)
  end

I've tried:

@program = @state.station.programs...
@program = @station.programs.create
@program = current_user.programs.create (without merging the hash)

ERROR MESSAGE: unknown attribute 'user' for Program.

Extracted source (around line #40):

  @state = State.find(params[:state_id])
  @station = Station.find(params[:station_id])
  **@program = @station.programs.create!(program_params.merge(user: current_user))**
  redirect_to state_path(@state)
  end

Thank you for your help!

juancgarcia commented 7 years ago

I think this may have to do with your model relations. Please provide a link to your project repo so we can have a closer look.

wjpurdum commented 7 years ago

Sorry about that - https://github.com/wjpurdum/Project2

juancgarcia commented 7 years ago

In your first example for add_favorite you are able to provide a user in @program.favorites.create(user: current_user) because the Favorite model has the relationship belongs_to :user app/models/favorite.rb#L2

The same approach fails for this create method because Program does not belong to User. Currently Program has_many :users, through: :favorites app/models/program.rb#L4

If you really want to have the Program "owned" by a user, you'll need to update your model and schema to show a belongs_to :user relationship first.

wjpurdum commented 7 years ago

OKay, that makes a lot of sense - thank you! I think I will instead go with a user having many comments, rather than tying the destroy/create to a program. In order to change my routes, would I do it like the below? (Insertion is in all caps.)

Rails.application.routes.draw do
  devise_for :users DO
    RESOURCES :COMMENTS
  END
  root to: "states#index"
  resources :favorites
  resources :states do
  resources :stations do
    resources :programs do
      post 'add_favorite'
      delete 'remove_favorite'
    end
    end
  end
end
juancgarcia commented 7 years ago

devise_for is only used to assign the model and path for your sign in/out actions. It doesn't take a do end block like that so leave devise_for :users by itself.

If you want to create a new nested route for /users/:id/comments/ we do that just as before by adding a nested resource:

resources :users do
    resources :comments
end

I don't see a Comment model in your pushed code. Is this something you already have on your local repo.

wjpurdum commented 7 years ago

I figured this out - thanks so much for your help!