【问题标题】:options_for_select on simple_formsimple_form 上的 options_for_select
【发布时间】:2016-06-27 18:42:33
【问题描述】:

我可能使用不正确,我无法弄清楚。

我在 simple_form 上使用 options_for_select。它呈现正常,没有错误,但所选选项不会保存到数据库中。所有其他字段保存没有问题。

select_tag 是

<%= select_tag :experiment_type, options_for_select(['AOV', 'Conversion']), :prompt => "Select a Project Type" %>  

控制器:

class ExperimentsController < ApplicationController
    before_action :find_advertiser
    before_action :find_experiment, only: [:edit, :update, :show, :destroy]


    def index
        @experiments = Experiment.all.order("created_at DESC")
    end

    def show

    end

    def new
        @experiment = Experiment.new
        @advertisers = Advertiser.all.map{ |c| [c.name, c.id] }

    end

    def create
        @experiment = Experiment.new(experiment_params)
        @experiment.advertiser_id = params[:advertiser_id]   

        if @experiment.save
             redirect_to advertiser_path(@advertiser)
        else
            render 'new'
        end
    end

    def edit
            @projects = Project.all.map{ |c| [c.name, c.id] }
    end

    def update
        @experiment.advertiser_id = params[:id]     
        if @experiment.update(experiment_params)
            redirect_to experiment_path(@experiment)
        else
            render 'edit'
        end
    end

    def destroy
        @experiment.destroy
        redirect_to root_path
    end

private

    def experiment_params
        params.require(:experiment).permit(:advertiser_id, :name, :experiment_type, :hypothesis, :priority, :status, :launch_date, 
            :description, :baseline_url, :test_url, :baseline_aov_60, :baseline_aov_30, :baseline_aov_mtd,
            :baseline_conversion_60, :baseline_conversion_30, :baseline_conversion_mtd)
    end

    def find_advertiser
        @advertiser = Advertiser.find(params[:advertiser_id])
    end

    def find_experiment
        @experiment = Experiment.find(params[:id])
    end
end

【问题讨论】:

  • 发布您的控制器代码
  • 好的,现在创建这个问题了吗?更新?两个都?也展示你的模型结构。控制台或日志中是否有任何错误?
  • 你能发布你得到的错误吗?您在迁移中为experiment_type 正确设置了数据类型吗?
  • 保存操作的服务器日志会很有用。
  • @WesFoster - 我认为问题出在创建操作期间。表单部分应该收集信息并将其发送到我的数据库。插入所有值,但experiment_type 为nil。 @Lahiru - 我的 experiment_type 数据类型设置为字符串。我认为这是正确的。查看 MySQL 数据库,它的 varchar (255) @gen - 我将粘贴到我的 Rails 控制台中。 Started POST 具有正确的 experiment_type 值,但 INSERT 根本不包含 experiment_type

标签: ruby-on-rails simple-form-for


【解决方案1】:

我需要你的完整表单布局来肯定地告诉你,但根据你的实验参数方法,实验类型字段是实验的一部分。但是,当您仅使用 select_tag 时,它不会连接到您的主对象。您只需使用select。类似这样:

<%= simple_form_for :experiment do |f| %>
...
<%= f.select ... %>
...
<% end %>

或在simple_form format:

<%= f.input :experiment_type, collection: ['AOV', 'Conversion'] %>

我的猜测也是基于你的哈希:

"experiment_type"=>"AOV", "experiment"=>{"name"=>"Test" ....

experiment_type 不在您的“实验”范围内。

【讨论】:

  • 哇!就是这样。所以我想我需要阅读 select vs select_tag 以了解差异。非常感谢您的帮助!
  • @LeoLeGendre:欢迎!那我猜你可以接受我的回答了。
  • @LeoLeGendre:你知道如何接受答案吗?在您的问题的每个答案下都有一个空白复选标记,您可以单击它来接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多