Ruby on rails 在Rails 4中创建特定于用户的参与页面
我有3个模型;考试、参与和用户。他们通过关系与许多人联系在一起,而参与模式是一种联合模式 我可以列出所有参与者或显示特定的参与者(请参见下面的“我的控制器”),但我的目标是创建一个名为“我的参与者”的页面,在该页面中,用户只能看到他们的参与者。参与模型有一个“user_id”字段用于此目的,但我不知道如何在controller中创建此类页面或方法 提前谢谢 以下是我的模型结构: examice.rb=>Ruby on rails 在Rails 4中创建特定于用户的参与页面,ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,我有3个模型;考试、参与和用户。他们通过关系与许多人联系在一起,而参与模式是一种联合模式 我可以列出所有参与者或显示特定的参与者(请参见下面的“我的控制器”),但我的目标是创建一个名为“我的参与者”的页面,在该页面中,用户只能看到他们的参与者。参与模型有一个“user_id”字段用于此目的,但我不知道如何在controller中创建此类页面或方法 提前谢谢 以下是我的模型结构: examice.rb=> class Examination < ActiveRecord::Base ha
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :bookings
has_many :exam_centers, :through => :bookings
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class User < ActiveRecord::Base
has_many :participations
has_many :examinations, :through => :participations
end
#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
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
devise_for :users
resources :examinations do
resources :participations do
member do
get :update_profile_information
get :approve_application
end
end
end
路线=>
class Examination < ActiveRecord::Base
has_many :participations
has_many :users, :through => :participations
has_many :bookings
has_many :exam_centers, :through => :bookings
end
class Participation < ActiveRecord::Base
belongs_to :user
belongs_to :examination
end
class User < ActiveRecord::Base
has_many :participations
has_many :examinations, :through => :participations
end
#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
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
devise_for :users
resources :examinations do
resources :participations do
member do
get :update_profile_information
get :approve_application
end
end
end
例如,您可以创建一个新的控制器,
myparticipationscocontroller
,然后创建index
操作来显示当前用户的参与
app/controllers/my_participations_controller.rb
class MyParticipationsController < ApplicationController
before_filter :authenticate_user!
def index
@participations = current_user.participations
end
end
如果需要,您可以删除参与控制器中的索引
操作