如何配置rails控制器,这样我就可以让用户在任何比赛中发布提交内容。当他们发布用户ID时,竞赛ID应自动附加到提交内容。
我知道我能做到:
有没有一种优雅的方法来解决这个问题?
提交控制器如下所示:
class SubmissionsController < ApplicationController
before_action :set_submission, only: [:show, :edit, :update, :destroy]
# the current user can only edit, update or destroy if the id of the pin matches the id the user is linked with.
before_action :correct_user, only: [:edit, :update, :destroy]
# the user has to authenticate for every action except index and show.
before_action :authenticate_user!, except: [:index, :show]
respond_to :html
def index
@title = t('submissions.index.title')
@submissions = Submission.all
respond_with(@submissions)
end
def show
@title = t('submissions.show.title')
respond_with(@submission)
end
def new
@title = t('submissions.new.title')
@submission = Submission.new
respond_with(@submission)
end
def edit
@title = t('submissions.edit.title')
end
def create
@title = t('submissions.create.title')
@submission = Submission.new(submission_params)
@submission.save
respond_with(@submission)
end
def update
@title = t('submissions.update.title')
@submission.update(submission_params)
respond_with(@submission)
end
def destroy
@title = t('submissions.destroy.title')
@submission.destroy
respond_with(@submission)
end
private
def set_submission
@submission = Submission.find(params[:id])
end
def submission_params
arams.require(:submission).permit(:reps, :weight, :user_id)
end
def correct_user
@submission = current_user.submissions.find_by(id: params[:id])
redirect_to submissions_path, notice: t('submissions.controller.correct_user') if @submission.nil?
end
end
我有以下模特:
class Contest < ActiveRecord::Base
has_many :submissions
has_many :users, through: :submissions
class Submission < ActiveRecord::Base
belongs_to :user
belongs_to :contest
class User < ActiveRecord::Base
has_many :submissions
has_many :contests, through: :submissions
答案 0 :(得分:0)
您是否可以在@submission = current_user.submissions.new(submission_params)
@contest = Contest.find(params[:contest_id])
和SubmissionsController#create
编辑:我已添加了一些有关在contest_id
表格中添加对submissions
的引用的详细信息。
我发现在Rails(实际上是任何关系数据库)中将相关事物联系在一起的最好方法是在子表中将引用添加到父id
。您可以在Rails中进行迁移。
rails g migration AddContestToSubmission contest:references
并修改db/migrate/<datetime>_add_contest_to_submission
中生成的迁移文件,使其类似于:
class AddContestToSubmission < ActiveRecord::Migration
def change
add_reference :submissions, :contest, index: true
end
end
然后继续查看submissions
中的schema.rb
表格。您应该注意到t.integer "contest_id"
之类的内容您可能还应该在迁移中添加user_id
,因为您希望将提交内容绑定到一个user
。
答案 1 :(得分:0)
我认为你让这有点复杂。
提交在比赛中张贴,提交需要知道user_id。
<%= simple_form_for :submission, url: contest_submissions_path(contest) do |f| %>
...
<%= f.submit 'Submit', class: "button" %>
<% end %>
并提交您的提交CREATE方法
class SubmissionsController < ApplicationController
def create
@contest = Contest.find(params[:contest_id])
@submission = @contest.submissions.new(submission_params)
@submissions.user = current_user
.....
end
魔术发生在@submissions.user = current_user
如果你正在使用Devise,很容易传入控制器中的current_user.id ANYWHERE,正如我在提交控制器中所做的那样。