Creating User Specific Participation Pages in Rails 4 -
i have 3 models; examinations, participation , user. connected has many through relationship - , participation model joint model.
i can list participations or show specific participation (see controller below) - goal create page named "my participations" - users can see participations. participation model has "user_id" field purpose don't know how create kind of page or method in controller.
thanks in advance.
here model structure:
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 =>
class participation < activerecord::base belongs_to :user belongs_to :examination end
user.rb =>
class user < activerecord::base has_many :participations has_many :examinations, :through => :participations end
as can see i'm having "participation" join model between examination , user models. have has many through rich relationship.
participations_controller.rb =>
#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 |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 |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 |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 |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 share common setup or constraints between actions. def set_participation @participation = participation.find(params[:id]) end # never trust parameters scary internet, allow white list through. def participation_params params.require(:participation).permit(:user_id, :examination_id, :payment_status, :language_preference, :exam_center_preference, :disability) end end
schema structure =>
create_table "examinations", force: true |t| t.string "name" t.string "shortname" t.datetime "exam_date" end create_table "participations", force: true |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 |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
routes =>
devise_for :users resources :examinations resources :participations member :update_profile_information :approve_application end end end
you can create new controller, myparticipationscontroller
example, , create index
action display participations current user.
app/controllers/my_participations_controller.rb
class myparticipationscontroller < applicationcontroller before_filter :authenticate_user! def index @participations = current_user.participations end end
config/routes.rb
resources :my_participations, only: [:index]
you may remove index
action in participationscontroller
if needed.
Comments
Post a Comment