【问题标题】:Select box not keeping param value when validation errors occur - Rails 3发生验证错误时选择框不保留参数值 - Rails 3
【发布时间】:2019-02-01 22:46:25
【问题描述】:

我遇到的问题是,如果发生验证错误,我的视图中的选择框不会保留选择的值。

我有一个多对多关联的业务和类别关系

category.rb

class Category < ActiveRecord::Base
    has_and_belongs_to_many :businesses

business.rb

class Business < ActiveRecord::Base
    has_and_belongs_to_many :categories

在我看来,我有一个选择字段

<%= f.select(:category_tokens, Category.all.collect {|c| [ c.name, c.id ] }, { :include_blank => "Select Category", :selected => params['category'] }}) %>

我可以使用返回和数组的business.categories 在控制台中访问业务类别。

在我看来是为了解决我添加的参数。

<%= @business.categories %>
<%= @business.attributes.inspect %>
<%= @business.user.attributes.inspect %>

这些节目的输出提供以下内容

[#<Category id: 2, name: "kitchen renovations", created_at: "2012-10-19 14:16:52", updated_at: "2012-10-19 14:16:52">]

{"id"=>nil, "business_name"=>"", ... additional attributes}

{"id"=>nil, "email"=>"", ... additional attributes}

params 哈希看起来像这样

Processing by BusinessesController#create as HTML
Parameters: {"utf8"=>"✓",
"authenticity_token"=>"fVz1mJZI8QT/HYKRph8YTvzRec0IVV0RS55v5QD5SL0=", 
"business"=>{"category_tokens"=>"2", 
"location"=>"", "service_area"=>"20", 
"business_name"=>"", "abn_number"=>"", 
"business_description"=>"", 
"address"=>"", "suburb"=>"", 
"notification_type"=>"", 
"user_attributes"=>{"first_name"=>"", "phone"=>"", "email"=>"", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}, "commit"=>"Sign up my Business"}

因此正在设置类别,但我不确定如何将其添加到视图中的选择中,以便在发生验证错误时将该类别用作选择值。

EDIT -- 添加控制器代码----

class BusinessesController < ApplicationController

  def new
    @business = Business.new
    @business.build_user
  end

  def create
    @business = Business.new(params[:business])
     respond_to do |format|
      if @business.save
         cookies[:auth_token] = @business.user.auth_token
      format.html { redirect_to jobs_users_path, notice: 'Your business was successfully created.' }
   else
     format.html { render action: "new" }
   end
 end

结束

【问题讨论】:

  • 你能提供提交发送到的控制器动作吗?
  • @CarsonCole,我已经添加了控制器代码。

标签: ruby-on-rails


【解决方案1】:

我通过一些简单的更改解决了这个问题。

在我的控制器中,我添加了以下内容

def create
  #...
  category = (params[:business][:category_tokens])
  @category = category
  #...
end

然后在我的视图中将其用于所选选项。

<%= f.select(:category_tokens, Category.all.collect {|c| [ c.name, c.id ] }, { :include_blank => "Select Category", :selected => @category }}) %>

【讨论】:

    【解决方案2】:

    从 2019 年开始更新,如果您的 f.select 看起来像这样:

    <%= f.select(:sector , options_for_select([["Please select", "Please select"], ["Energy", "Energy"], ["Metals","Metals"], ["Agriculture","Agriculture"], ["Renewables","Renewables"], ["Natural Ressources","Natural Ressources"], ["Other","Other"]])) %>
    

    您所要做的就是删除 options_for_select( ... ),如果发生验证错误,页面将“记住”您选择的内容。

    <%= f.select(:sector , [["Please select", "Please select"], ["Energy", "Energy"], ["Metals","Metals"], ["Agriculture","Agriculture"], ["Renewables","Renewables"], ["Natural Ressources","Natural Ressources"], ["Other","Other"]]) %>
    

    我不知道这如何与多选输入一起使用。为了提高可读性,我还使用了自己的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 2021-02-24
      • 1970-01-01
      相关资源
      最近更新 更多