Ruby Rails 用户只能编辑首选项,不能查看列表或创建新的

Ruby on Rails User can only edit a preference, not see list or create new

我在我的 Ruby on Rails 应用程序中添加用户首选项,我目前正在尝试做的是让用户只能编辑他们的首选项,而不是创建一个新的或查看每个人的偏好列表。这需要在使用首选项 table.

的 user_id 列中出现的用户 table 的 ID 创建新用户时进行新的首选项

这是架构:

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

还有我的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]

请注意,路由中注释掉的行是我试图获取的内容,这样用户就不会转到 perferences/index 页面,而是被发送到 perferences/edit 页面,在那里他们可以编辑自己的自己的喜好。

澄清我需要做什么:

有人可以帮忙吗。

用户控制器:

 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
     @user.add_default_preferences # adds default preferences
     redirect_to films_path
  else
     render action: "new"
  end
end

用户模型:

class User < ActiveRecord::Base

   def add_default_preferences
      Preference.create user_id: self.id # creates 1 preference for user
   end

end

路线:

# resources :perferences
# just add route for edit
get 'preferences/:id' => 'preferences#edit', as: :edit_preference # route for editing
patch 'preferences/:id' => 'preferences#update' # route for updating
# just implement your preferences controller
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

然后在你的视图中添加

   <% if @preference %>
    <%= link_to "My Preferences" , edit_preference_path(@preference) %>
   <% end %>

在您的 Preference 控制器过滤器中,只有首选项所有者可以来编辑首选项。