2016-12-07 33 views
0

我有屬於提交的學生,我需要一個顯示學生的下拉列表,以便用戶可以選擇其中一個。 我試圖用這個,但它不工作:DropDown與另一個模型的名字欄杆

<%= collection_select(:student_name, :submission_id, Submission.all, :form_id, :title) %> 

submission.rb:

class Submission < ActiveRecord::Base 
    belongs_to :form 
    has_one :student 
end 

student.rb:

class Student < ActiveRecord::Base 
    belongs_to :submission 
end 

submission_controller.rb:

class SubmissionsController < ApplicationController 
    before_action :set_submission, only: [:show, :edit, :update, :destroy] 
    before_action :set_form 

    # GET /submissions/new 
    def new 
    @submission = Submission.new 
    end 

    # GET /submissions/1/edit 
    def edit 
    end 

    # POST /submissions 
    # POST /submissions.json 
    def create 
    @submission = Submission.new(submission_params) 
    @submission.form_id = @form.id 

    respond_to do |format| 
     if @submission.save 
     format.html { redirect_to @form, notice: 'Submission was successfully created.' } 
     format.json { render :show, status: :created, location: @submission } 
     else 
     format.html { render :new } 
     format.json { render json: @submission.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /submissions/1 
    # PATCH/PUT /submissions/1.json 
    def update 
    respond_to do |format| 
     if @submission.update(submission_params) 
     format.html { redirect_to @submission, notice: 'Submission was successfully updated.' } 
     format.json { render :show, status: :ok, location: @submission } 
     else 
     format.html { render :edit } 
     format.json { render json: @submission.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /submissions/1 
    # DELETE /submissions/1.json 
    def destroy 
    @submission.destroy 
    respond_to do |format| 
     format.html { redirect_to submissions_url, notice: 'Submission was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_submission 
     @submission = Submission.find(params[:id]) 
    end 

    def set_form 
     @form = Form.find(params[:form_id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def submission_params 
     params.require(:submission).permit(:conflict, :computer, :extra_time, :am_pm) 
    end 
end 

student_con troller.rb:

class StudentsController < ApplicationController 
    before_action :set_student, only: [:show, :edit, :update, :destroy] 

    # GET /students 
    # GET /students.json 
    def index 
    @students = Student.all 
    end 

    # GET /students/1 
    # GET /students/1.json 
    def show 
    end 

    # GET /students/new 
    def new 
    @student = Student.new 
    end 

    # GET /students/1/edit 
    def edit 
    end 

    # POST /students 
    # POST /students.json 
    def create 
    @student = Student.new(student_params) 

    respond_to do |format| 
     if @student.save 
     format.html { redirect_to @student, notice: 'Student was successfully created.' } 
     format.json { render :show, status: :created, location: @student } 
     else 
     format.html { render :new } 
     format.json { render json: @student.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # PATCH/PUT /students/1 
    # PATCH/PUT /students/1.json 
    def update 
    respond_to do |format| 
     if @student.update(student_params) 
     format.html { redirect_to @student, notice: 'Student was successfully updated.' } 
     format.json { render :show, status: :ok, location: @student } 
     else 
     format.html { render :edit } 
     format.json { render json: @student.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    # DELETE /students/1 
    # DELETE /students/1.json 
    def destroy 
    @student.destroy 
    respond_to do |format| 
     format.html { redirect_to students_url, notice: 'Student was successfully destroyed.' } 
     format.json { head :no_content } 
    end 
    end 

    private 
    # Use callbacks to share common setup or constraints between actions. 
    def set_student 
     @student = Student.find(params[:id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def student_params 
     params.require(:student).permit(:student_name) 
    end 
end 

提交表單:

<%= form_for([@form, @submission]) do |f| %> 
    <% if @submission.errors.any? %> 
    <div id="error_explanation"> 
     <h2><%= pluralize(@submission.errors.count, "error") %> prohibited this submission from being saved:</h2> 

     <ul> 
     <% @submission.errors.full_messages.each do |message| %> 
      <li><%= message %></li> 
     <% end %> 
     </ul> 
    </div> 
    <% end %> 

    <div class="field"> 
    <%= f.label :conflict %><br /> 
    <%= f.radio_button :conflict, :Yes, required: :true %> Yes<br> 
    <%= f.radio_button :conflict, :No, required: :true %> No 
    </div> 
    <br> 
    <div class="field"> 
    <%= f.label :computer %><br /> 
    <%= f.radio_button :computer, :Yes, required: :true %> Yes<br> 
    <%= f.radio_button :computer, :No, required: :true %> No 
    </div> 
    <br> 
    <div class="field"> 
    <%= f.label :am_pm %><br /> 
    <%= f.radio_button :am_pm, :Am, required: :true %> Am<br> 
    <%= f.radio_button :am_pm, :Pm, required: :true %> Pm 
    </div> 
    <br> 
    <div class="field"> 
    <%= f.label "Student" %><br /> 
    <%= collection_select(:student_name, :submission_id, Submission.all, :form_id, :title) %> 
    </div> 
    <br> 
    <div class="field"> 
    <%= f.label :extra_time %><br /> 
    <%= f.radio_button :extra_time, 25, required: :true %> 25%<br> 
    <%= f.radio_button :extra_time, 50, required: :true %> 50% 
    </div> 
    <br> 
    <div class="actions"> 
    <%= f.submit "Submit", class: "btn btn-primary" %> 
    </div> 
<% end %> 

希望找到一個答案!謝謝

+0

您要提交一些數據,哪裏是你的形式?添加你的控制器代碼。看到你的'collection_select',看起來你試圖顯示'Submission'的'title'?但是你提到你需要一個顯示學生的下拉菜單! – dp7

回答

0

我假設你有一個提交表單,你想讓學生在它下面下拉。

如果是這種情況,請執行下列操作 -

<%= f.select(:student_id, Student.all.collect { |s| [s.name, s.id] }, {:include_blank => true, :selected => f.object.student_id}) %> 

做以上,名稱將顯示在下拉列表和ID將與選項標籤進行綁定。

有關詳細信息,請查看本 - http://apidock.com/rails/ActionView/Helpers/FormBuilder/select

0

我找到了答案:

<%= collection_select(:student, :student_id, Student.all, :id, :student_name, prompt: true) %> 

謝謝大家

相關問題