如何通过连接表在has_many中保存布尔条目

时间:2016-07-09 15:30:53

标签: ruby-on-rails has-many-through

我需要在LessonsController中定义一个方法/操作,我可以从课程显示操作调用该操作,该操作将课程标记为当前用户已完成。该控制器方法是什么样的?

以下是我的模型概述:

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

我的完成表如下所示:

   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

这是我的按钮链接,以实现此功能。

<%= button_to "Mark this Lesson as Complete", complete_lesson_path(@lesson), method: :put, class: "btn btn-warning btn-lg" %>

这会起作用还是我的意思?谢谢!

3 个答案:

答案 0 :(得分:1)

请在完成控制器中尝试使用以下代码

Bitmap cameraBitmap =BitmapFactory.decodeByteArray(data, 0, data.length);
Bitmap overlayBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.snow);

Bitmap cameraScaledBitmap = Bitmap.createScaledBitmap(cameraBitmap, 1200, 1600, true);





        int wid = cameraScaledBitmap.getWidth();
        int hgt = cameraScaledBitmap.getHeight();
        Bitmap newImage = Bitmap.createBitmap(wid,hgt, Bitmap.Config.ARGB_8888);



        Bitmap overlayScaledBitmap = Bitmap.createScaledBitmap(overlayBitmap, wid, hgt, true);
        Canvas canvas = new Canvas(newImage);
        canvas.drawBitmap(cameraBitmap , 0, 0, null);
        canvas.drawBitmap(overlayScaledBitmap , 0, 0, null);

答案 1 :(得分:1)

保持这是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

答案 2 :(得分:0)

您也可以将user: current_user传递给completions.create方法,而不是传递current_user.id

@lesson.completions.create(completed_step: true, user: current_user)

这样的东西