【发布时间】:2015-03-31 12:54:24
【问题描述】:
我正在我的 Ruby on Rails 应用程序中添加用户首选项,而我目前正在尝试做的是让用户只能编辑他们的首选项,而不是创建新的首选项或查看每个人的首选项列表.这需要在使用用户表中的 id 出现在首选项表的 user_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]
请注意,路线中注释掉的行是我试图获取的内容,因此用户不会转到首选项/索引页面,而是将用户发送到他们编辑自己的首选项的首选项/编辑页面。
澄清我需要做什么:
- 使用户无法转到首选项/索引或首选项/新页面,而是被发送到他们编辑现有首选项的首选项/编辑页面
- 在创建新用户时创建新的首选项,以便他们可以编辑首选项,即使到目前为止除了他们的 user_id 之外什么都没有
有人可以帮忙吗。
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-3 routes updates