Collection_select没有将值传递给insert

时间:2016-06-29 06:14:04

标签: ruby

我在从下拉列表中保存数据时遇到问题 下面我有表格上的字段和随附的日志

此设置正常工作,您可以通过日志插入

看到
<div class="field">
    <%= f.label :company_id %><br>
    <%= f.number_field :company_id %>
  </div>

日志文件

INSERT INTO "learners"
 ("learner_id",  "company_id", "id_type", "id_number", "first_name", "last_name", "middle_names", "title", "equity", "gender", "date_of_birth", "created_at", "updated_at") 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  
[["learner_id", 1], ["company_id", 1], ["id_type", "Book"], ["id_number", 33], ["first_name", "Jack"], ["last_name", "Pillay"], ["middle_names", ""], ["title", "Mr"],
 ["equity", "Black"], ["gender", "Male"], ["date_of_birth", "2016-06-28"], 
["created_at", "2016-06-28 19:16:41.969037"], ["updated_at", "2016-06-28 19:16:41.969037"]]

Dropdown有效,但没有传递任何值

<div class="field">
    <%= f.label :company_id %><br>
    <%= collection_select(:learner, :learner_id, Client.all, :company_id, :name, prompt: true) %> 
  </div>

日志文件 - 查看缺少的company_id

INSERT INTO "learners"
 ("learner_id", "id_type", "id_number", "first_name", "last_name", "middle_names", "title", "equity", "gender", "date_of_birth", "created_at", "updated_at") 
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)  
[["learner_id", 1], ["id_type", "Book"], ["id_number", 33], ["first_name", "Jack"], ["last_name", "Pillay"], ["middle_names", ""], ["title", "Mr"],
 ["equity", "Black"], ["gender", "Male"], ["date_of_birth", "2016-06-28"], 
["created_at", "2016-06-28 19:16:41.969037"], ["updated_at", "2016-06-28 19:16:41.969037"]]

学习者控制器

class LearnersController < ApplicationController
  before_action :set_learner, only: [:show, :edit, :update, :destroy]

  # GET /learners
  # GET /learners.json
  def index
    @learners = Learner.all
  end

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

  # GET /learners/new
  def new
    @learner = Learner.new
    @clients = Client.all
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @learner }
    end
  end

  # GET /learners/1/edit
  def edit
  end

  # POST /learners
  # POST /learners.json
  def create
    @learner = Learner.new(learner_params)

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

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

  # DELETE /learners/1
  # DELETE /learners/1.json
  def destroy
    @learner.destroy
    respond_to do |format|
      format.html { redirect_to learners_url, notice: 'Learner was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def learner_params
      params.require(:learner).permit(:learner_id, :company_id, :id_type, :id_number, :date_of_birth, :first_name, :last_name, :middle_names, :title, :equity, :gender)
    end
end

1 个答案:

答案 0 :(得分:0)

这一行

<%= collection_select(:learner, :learner_id, Client.all, :company_id, :name, prompt: true) %>

告诉Rails将选定的值发送到params[:learner][:learner_id]中的服务器。如果要将该值用作client_id,则应将该行更改为:

<%= collection_select(:learner, :company_id, Client.all, :company_id, :name, prompt: true) %> 

或者使用稍短的FormBuilder版本:

<%= f.collection_select(:company_id, Client.all, :company_id, :name, prompt: true) %>