我有3个型号;考试,参与和用户。它们之间存在着许多关系 - 而参与模型是一种联合模式。
我可以列出所有参与或显示特定参与(请参阅下面的我的控制器) - 但我的目标是创建一个名为“我的参与”的页面 - 用户只能看到他们的参与。参与模型有一个“user_id”字段用于此目的,但我不知道如何在控制器中创建这种页面或方法。
提前致谢。
这是我的模型结构:
examination.rb =>
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :bookings
has_many :exam_centers, :through => :bookings
end
participation.rb =&gt;
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
user.rb =&gt;
class User < ActiveRecord::Base
has_many :participations
has_many :examinations, :through => :participations
end
正如您所看到的,我将“参与”作为考试和用户模型之间的联接模型。他们有很多通过丰富的关系。
participations_controller.rb =&gt;
#encoding: utf-8
class ParticipationsController < ApplicationController
before_filter :authenticate_user!
before_action :set_participation, only: [:show, :edit, :update, :destroy]
before_filter :get_examination
def get_examination
@examination = Examination.find(params[:examination_id])
end
def index
@participations = @examination.participations
respond_to do |format|
format.html
format.csv { send_data @participations.to_csv }
format.xls # { send_data @examinations.to_csv(col_sep: "\t") }
end
end
def show
@participation = @examination.participations.find(params[:id])
end
def new
@participation = Participation.new
end
def edit
end
def create
@participation = @examination.participations.new(participation_params)
@participation.user = current_user
respond_to do |format|
if @participation.save
format.html { redirect_to [@examination, @participation], notice: 'Başvuru işleminiz başarıyla tamamlandı! Lütfen başvurunuzu kontrol ediniz ve onaylayınız. Onaylanmamış başvurular geçersiz sayılacaktır!' }
format.json { render action: 'show', status: :created, location: [@examination, @participation] }
else
render 'new'
format.html { render action: 'new' }
format.json { render json: @participation.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @participation.update(participation_params)
format.html { redirect_to [@examination, @participation], notice: 'Başvurunuz Başarıyla Güncellendi! Lütfen başvurunuzu kontrol ediniz ve onaylayınız. Onaylanmamış başvurular geçersiz sayılacaktır!' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: participation.errors, status: :unprocessable_entity }
end
end
end
def destroy
@participation.destroy
respond_to do |format|
format.html { redirect_to examination_participations_path }
format.json { head :no_content }
end
end
def approve_application
@participation = @examination.participations.find(params[:id])
@participation.is_approved = true
@participation.save
redirect_to examination_participation_path, notice: 'Kaydınızı Onayladınız! Tebrikler!'
end
private
# Use callbacks to share common setup or constraints between actions.
def set_participation
@participation = Participation.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def participation_params
params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability)
end
end
架构结构=&gt;
create_table "examinations", force: true do |t|
t.string "name"
t.string "shortname"
t.datetime "exam_date"
end
create_table "participations", force: true do |t|
t.integer "user_id"
t.integer "examination_id"
t.boolean "payment_status"
t.string "language_preference"
t.string "exam_center_preference"
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
t.string "first_name", default: "", null: false
t.string "last_name", default: "", null: false
t.string "id_number"
end
路线=&gt;
devise_for :users
resources :examinations do
resources :participations do
member do
get :update_profile_information
get :approve_application
end
end
end
答案 0 :(得分:1)
您可以创建一个新控制器,例如MyParticipationsController
,并创建index
操作以显示当前用户的参与。
应用程序/控制器/ my_participations_controller.rb
class MyParticipationsController < ApplicationController
before_filter :authenticate_user!
def index
@participations = current_user.participations
end
end
配置/ routes.rb中
resources :my_participations, only: [:index]
如果需要,您可以删除index
中的ParticipationsController
操作。