Rails FormBuilder 选择使用“奇数”设置选定值协会

发布于 2024-12-02 16:41:31 字数 1753 浏览 2 评论 0原文

我遇到了这种稍微偏左的情况,我无法工作。最简单的问题是,我使用 has_one/belongs_to 关联构建此选择,并且它填充得很好,在提交时返回有效值,但是如果提交因某种原因失败(例如,不同的输入验证失败),那么当表单为使用错误消息重新绘制,选择默认为列表中的第一个值而不是选定的值...这是一些上下文。

我有以下两种型号。第一个是我从 PHP 应用程序继承的表,必须“玩得很好”,因此它不遵循 Rails 约定:

class Listing < ActiveRecord::Base
  set_primary_key :lid
  has_one :site
end

class Site < ActiveRecord::Base
  belongs_to :listing
end

您可以看到我必须跳过一个小环来覆盖这个旧表使用“lid”的事实" 而不是 "id" 作为其主键。

在我看来,我使用 ActionView::Helpers::FormBuilder::select 来选择此表单:

.field
    .left.form-label
      = f.label :listing
    .left.form-field
      = f.select( :listing_id, options_from_collection_for_select(Listing.all.sort {|a,b| a.address <=> b.address}, :lid, :address), :prompt => "Please select an address", { :selected => @site.listing_id })

当我执行提交并由于其他不相关的原因返回错误时,选择设置为列表中的第一个选项,而不是用户实际选择的一个。

其他一些上下文。以下是注定会首先失败的保存中的传入参数:

Parameters: {"utf8"=>"✓", 
  "authenticity_token"=>"X/yqRO0VA0/3GrkGT0sc2KVPd4sVLF2Nd/vnjQM7GOI=", "site"=>
  {"domain"=>"butterballasshat.info", "listing_id"=>"937", "user_id"=>"21", 
  "description"=>"asdf"}, "commit"=>"Save"}

这是相关的控制器代码:

def create
  @site = Site.new(params[:site])

  respond_to do |format|
    if success and @site.save
      format.html { redirect_to(sites_url, :notice => 'Site was successfully created.') }
      format.xml  { render :xml => sites_url, :status => :created, :location => @site }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @site.errors, :status => :unprocessable_entity }
    end
  end
end

如果您需要任何其他信息来解决我的问题,我将很乐意提供它...

I've got this slightly left of center situation that I can't get working. The problem in simplest terms is that I build this select using a has_one/belongs_to association and it populates fine, returns valid values on a submit, BUT if the submit fails for some reason (a different input validation fails for example) when the form is repainted with the error message, the select defaults to the first value in the list rather than the one selected...Here's some context.

I have the following two models. The first one is a table that I inherited from a PHP app and have to "play nice" with so it's not following rails convention:

class Listing < ActiveRecord::Base
  set_primary_key :lid
  has_one :site
end

class Site < ActiveRecord::Base
  belongs_to :listing
end

You can see I had to jump through a tiny hoop to cover the fact that this legacy table uses "lid" instead of "id" as its primary key.

In my view I have this form select using ActionView::Helpers::FormBuilder::select:

.field
    .left.form-label
      = f.label :listing
    .left.form-field
      = f.select( :listing_id, options_from_collection_for_select(Listing.all.sort {|a,b| a.address <=> b.address}, :lid, :address), :prompt => "Please select an address", { :selected => @site.listing_id })

When I do a submit and it returns an error for some other unrelated reason, the select is set to the first option in the list, not the one that the user actually selected.

A few other bits of context. Here are the incoming params on the save that is destined to fail first:

Parameters: {"utf8"=>"✓", 
  "authenticity_token"=>"X/yqRO0VA0/3GrkGT0sc2KVPd4sVLF2Nd/vnjQM7GOI=", "site"=>
  {"domain"=>"butterballasshat.info", "listing_id"=>"937", "user_id"=>"21", 
  "description"=>"asdf"}, "commit"=>"Save"}

And here is the relevant controller code:

def create
  @site = Site.new(params[:site])

  respond_to do |format|
    if success and @site.save
      format.html { redirect_to(sites_url, :notice => 'Site was successfully created.') }
      format.xml  { render :xml => sites_url, :status => :created, :location => @site }
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @site.errors, :status => :unprocessable_entity }
    end
  end
end

If you need any other information to wrap your head around my problem I will gladly provide it...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半世蒼涼 2024-12-09 16:41:31

按要求:

不确定问题是什么,没有看到错误,但要解决您的选择问题,请将所选内容添加到 options_from_collection_for_select 方法中,结构如下:options_from_collection_for_select(collection, value 、标签、selected_id)

as requested :

Not sure what the problem is without seeing the error, but to solve your select issue, add the selected stuff to the options_from_collection_for_select method, in the following structure: options_from_collection_for_select(collection, value, label, selected_id)

浪推晚风 2024-12-09 16:41:31

现在这是一种更简单的方法:

<%= f.collection_select(:city_id, City.all, :id, :name) %>

This is an easier way to do this now:

<%= f.collection_select(:city_id, City.all, :id, :name) %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文