I am adding in user preferences in my Ruby on Rails application, and what I am trying to currently do is make it so that a user can only edit their preferences, so not create a new one or view a list of everybody's preferences. This requires a new preference to be made when a new user is made with the id from the users table appearing in the user_id column in the preferences table.
This is the schema:
create_table "perferences", force: :cascade do |t|
t.integer "user_id"
t.integer "category_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.text "colour"
end
create_table "users", force: :cascade do |t|
t.string "password_digest"
t.string "role"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "first_name"
t.string "last_name"
t.string "house_no"
t.string "street"
t.string "town"
t.string "postcode"
t.string "email"
t.date "date_of_birth"
end
Users_controller:
def create
@user = User.new(user_params)
if @user.save
# login is achieved by saving a user's 'id' in a session variable,
# accessible to all pages
session[:user_id] = @user.id
UserMailer.welcome_email(@user).deliver_now
redirect_to films_path
else
render action: "new"
end
end
And my routes.rb:
resources :perferences
resources :prices
# get 'perferences/index', post: 'perferences#new'
# post 'perferences/index', to: 'perferences#new'
# match 'perferences' => 'perferences#create', :via => [:post]
post 'reviews/new/:id', to: 'reviews#new'
get 'reviews/new/:id', to: 'reviews#new'
#resources :showings
get 'sessions/new'
get 'sessions/create'
get 'sessions/destroy'
controller :sessions do
get 'login' => :new
post 'login' => :create
get 'logout' => :destroy
end
#post 'films/search', to: 'films#search'
get 'films/index'
get 'categories/index'
get 'certificates/index'
root 'films#index'
post 'films/search', to: 'films#search'
post 'films/search_1', to: 'films#search_1'
post 'films/search_2', to: 'films#search_2'
post 'films/search_3', to: 'films#search_3'
post 'films/search_4', to: 'films#search_4'
post 'films/search_5', to: 'films#search_5'
post 'films/search_6', to: 'films#search_6'
post 'films/display_products_by_genre', :to => 'films#display_products_by_genre'
get 'films/display_products_by_genre', :to => 'films#display_products_by_genre'
post 'seats/display_seats_by_screen', :to => 'seats#display_seats_by_screen'
get 'seats/display_seats_by_screen', :to => 'seats#display_seats_by_screen'
post 'screens/display_screens_by_showing', :to => 'screens#display_screens_by_showing'
get 'screens/display_screens_by_showing', :to => 'screens#display_screens_by_showing'
post 'films/multi_find', :to => 'films#multi_find'
get 'films/multi_find', :to => 'films#multi_find'
post 'seats/multi_find', :to => 'seats#multi_find'
get 'seats/multi_find', :to => 'seats#multi_find'
post 'screens/multi_find', :to => 'screens#multi_find'
get 'screens/multi_find', :to => 'screens#multi_find'
resources :users
resources :films
resources :categories
resources :certificates
resources :showings
resources :bookings
resources :seats
resources :screens
resources :reviews
resources :films do
resources :showings
end
resources :showings do
resources :seats
end
match '*a', to: 'errors#routing', via: [:get, :post]
Note the commented out lines in the routes are what I have tried to get it so that rather than going to the perferences/index page, the user is sent to perferences/edit page where they edit their own perferences.
To clarify what I need to do:
- Make it so that a user cannot go to the perferences/index or perferences/new page but are sent to the perferences/edit page where they edit their existing preferences
- Create a new preference when a new user is created so that they have a preference to edit, even if it has nothing but their user_id so far
Can someone please help.
Aucun commentaire:
Enregistrer un commentaire