如何通过连接 Table 在 has_many 中保存布尔值条目
How to save a Boolean entry in a has_many through join Table
我需要在我的 LessonsController 中定义一个 method/action,我可以从课程显示操作中调用它,该操作将课程标记为当前用户已完成。该控制器方法是什么样的?
这是我的模型的概述:
User
has_many :completions
has_many :completed_steps, through: :completions, source: :lesson
Lesson
has_many :completions
has_many :completed_by, through: :completions, source: :user
Completions
belongs_to :user
belongs_to :lesson
我的完成 Table 看起来像这样:
t.integer "user_id"
t.integer "lesson_id"
t.boolean "completed_step"
t.string "completed_by"
我假设 LessonsController 看起来像这样
def complete_step
self.completions.create(completed_step: true, user_id: current_user.id)
end
路线信息:
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :coupons
resources :lessons
resources :plans
resources :roles
resources :subscriptions
resources :completions
root to: "users#index"
end
devise_for :users, :controllers => { :registrations => "registrations"}
# Added by Koudoku.
mount Koudoku::Engine, at: 'koudoku'
scope module: 'koudoku' do
get 'pricing' => 'subscriptions#index', as: 'pricing'
end
resources :lessons do
member :complete
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#home'
get '/dashboard' => 'pages#dashboard', :as => 'dashboard'
mount StripeEvent::Engine, at: '/stripe-events' # prov
end
这是我的按钮 link 以实现此功能。
<%= button_to "Mark this Lesson as Complete", complete_lesson_path(@lesson), method: :put, class: "btn btn-warning btn-lg" %>
这行得通吗?我还差得远吗?谢谢!
请尝试使用以下 code.in 您的完成控制器
def create
@lession = Lession.find(params[:lession_id])
@completion = current_user.completions.create(completed_step: true, lesson_id: @lesson.id)
redirected_to @completion
end
保留 LessonsController,但可以通过以下任一方式更改它:
def complete_step
current_user.completions.create(completed_step: true, lesson_id: @lesson.id)
end
# ~~ OR ~~
def complete_step
@lesson.completions.create(completed_step: true, user_id: current_user.id)
end
这两个假设您已经在控制器中设置了 @lesson
,可能是在 before_action :set_lesson
.
中
编辑:
如果您需要路线建议,假设您的路线文件中有 resources :lessons
,您可以使用现有路线(可能 update
)或像这样添加成员路线:
resources :lessons do
get 'complete', on: :member
end
如果添加路由,则需要添加类似于
的操作
def complete
complete_step
redirect @lesson
end
或类似的,但是你想自己处理响应。您还需要确保 @lesson
已设置,因此您应该调整 before_action :set_lesson, only: [:show, :update, ...]
以包含 :complete
您也可以只将 user: current_user
传递给 completions.create
方法,而不是传递 current_user.id
类似于@lesson.completions.create(completed_step: true, user: current_user)
我需要在我的 LessonsController 中定义一个 method/action,我可以从课程显示操作中调用它,该操作将课程标记为当前用户已完成。该控制器方法是什么样的?
这是我的模型的概述:
User
has_many :completions
has_many :completed_steps, through: :completions, source: :lesson
Lesson
has_many :completions
has_many :completed_by, through: :completions, source: :user
Completions
belongs_to :user
belongs_to :lesson
我的完成 Table 看起来像这样:
t.integer "user_id"
t.integer "lesson_id"
t.boolean "completed_step"
t.string "completed_by"
我假设 LessonsController 看起来像这样
def complete_step
self.completions.create(completed_step: true, user_id: current_user.id)
end
路线信息:
Rails.application.routes.draw do
namespace :admin do
resources :users
resources :coupons
resources :lessons
resources :plans
resources :roles
resources :subscriptions
resources :completions
root to: "users#index"
end
devise_for :users, :controllers => { :registrations => "registrations"}
# Added by Koudoku.
mount Koudoku::Engine, at: 'koudoku'
scope module: 'koudoku' do
get 'pricing' => 'subscriptions#index', as: 'pricing'
end
resources :lessons do
member :complete
end
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
root 'pages#home'
get '/dashboard' => 'pages#dashboard', :as => 'dashboard'
mount StripeEvent::Engine, at: '/stripe-events' # prov
end
这是我的按钮 link 以实现此功能。
<%= button_to "Mark this Lesson as Complete", complete_lesson_path(@lesson), method: :put, class: "btn btn-warning btn-lg" %>
这行得通吗?我还差得远吗?谢谢!
请尝试使用以下 code.in 您的完成控制器
def create
@lession = Lession.find(params[:lession_id])
@completion = current_user.completions.create(completed_step: true, lesson_id: @lesson.id)
redirected_to @completion
end
保留 LessonsController,但可以通过以下任一方式更改它:
def complete_step
current_user.completions.create(completed_step: true, lesson_id: @lesson.id)
end
# ~~ OR ~~
def complete_step
@lesson.completions.create(completed_step: true, user_id: current_user.id)
end
这两个假设您已经在控制器中设置了 @lesson
,可能是在 before_action :set_lesson
.
编辑:
如果您需要路线建议,假设您的路线文件中有 resources :lessons
,您可以使用现有路线(可能 update
)或像这样添加成员路线:
resources :lessons do
get 'complete', on: :member
end
如果添加路由,则需要添加类似于
的操作def complete
complete_step
redirect @lesson
end
或类似的,但是你想自己处理响应。您还需要确保 @lesson
已设置,因此您应该调整 before_action :set_lesson, only: [:show, :update, ...]
以包含 :complete
您也可以只将 user: current_user
传递给 completions.create
方法,而不是传递 current_user.id
类似于@lesson.completions.create(completed_step: true, user: current_user)