Closed wjpurdum closed 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.
Sorry about that - https://github.com/wjpurdum/Project2
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.
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
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.
I figured this out - thanks so much for your help!
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:
This does not:
I've tried:
ERROR MESSAGE: unknown attribute 'user' for Program.
Extracted source (around line #40):
Thank you for your help!