【问题标题】:option :prompt=>true from f.select not working on edit action选项 :prompt=>true 来自 f.select 不适用于编辑操作
【发布时间】:2012-01-17 18:01:48
【问题描述】:

我的编辑表单有点问题。由于某种原因,我的选择助手无法在编辑操作上正常工作。由于某种原因,它似乎忽略了选项 :prompt=>true 并且不显示“请选择”消息。但只有在编辑操作上,新操作才能正常工作。

部分形式的sn-p:

f.select :category_id, @categories, {:prompt => true}

编辑动作

  def edit
    @page = Page.find(params[:id])
    @categories = Category.where(:cat_type=>"page").map { |c| [t("category.#{c.slug}",:default=>"#{c.name}"), c.id] }
  end

新动作

  def new
    @page = Page.new
    @categories = Category.where(:cat_type=>"page").map { |c| [t("category.#{c.slug}",:default=>"#{c.name}"), c.id] }

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @page }
    end
  end

有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails-3.1 erb


    【解决方案1】:
    f.select :category_id, @categories, :include_blank => "whatever your prompt says"
    

    这对我有用。

    但是,我通常会尽量避免使用 rails 表单助手进行提示,因为它们的行为似乎永远不会正确。

    或者,我是这样做的:

    f.select :category_id, @categories.unshift(["whatever your prompt says", value])
    

    将数组推到@categories 数组的前面

    【讨论】:

    • 不错,我要花很长时间才能找到第一个解决方案!
    • 我得到这个错误 undefined local variable or method `value' for
    【解决方案2】:

    来自 Apidock:

    select(object, method, choices, options = {}, html_options = {})
    

    这样就可以了:

    f.select :category_id, options_from_collection_for_select(@categories, :id, :name), {prompt: 'Please select ...' }, { class: 'form-control' }
    

    【讨论】:

      【解决方案3】:

      它按预期工作。如果之前选择并保存了一个值,它不会显示提示。

      Prompt vs. Select -- “主要区别在于,如果 select 已经有值,则 :prompt 不会显示,而 :include_blank 总是会显示。”

      请参阅this API note

      【讨论】:

        【解决方案4】:

        为具有提示值和 html 选项类的方法选择表单选项:

        f.select(:subcription_with, options_for_select(['paypal', 'stripe']), {:prompt => 'Select Payment Method'}, {:class => "form-control slct-box"})
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-26
          • 2019-05-14
          • 1970-01-01
          • 2022-08-19
          相关资源
          最近更新 更多