Ruby on Rails Collection 选择 - 如何预先选择正确的值? [英] Ruby on Rails Collection select - how to pre-select the right value?

查看:35
本文介绍了Ruby on Rails Collection 选择 - 如何预先选择正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去三天我都在研究集合 _ 为我的列表"选择表单助手 - 表单,用户可以在其中选择一个类别.

I spent the last three days working on the collection _ select form helper for my "listing" - form, where users can select a category.

我希望将listing.category_id 中当前设置的类别作为预选值.

I would like to have the category currently set in listing.category_id as the preselected value.

我的视图代码如下所示:

My view code looks like this:

<%= l.collection_select(:category_id, @category, :id, :name, options = {},
                        html_options = {:size => 10, :selected => @listing.category_id.to_s})%>

我知道这是不正确的,但即使阅读了 Shiningthrough (http://shiningthrough.co.uk/blog/show/6) 我不明白如何继续.

I know this is not correct, but even reading looking at the explanation from Shiningthrough (http://shiningthrough.co.uk/blog/show/6) I can not understand how to proceed.

感谢您的支持,

迈克尔

查看:如上
控制器:

def categories #Step 2
@listing = Listing.find(params[:listing_id])
@seller = Seller.find(@listing.seller_id)
@category = Category.find(:all)
@listing.complete = "step1"

respond_to do |format|
  if @listing.update_attributes(params[:listing])
    flash[:notice] = 'Step one succesful. Item saved.'
    format.html #categories.html.erb
end
end
end

推荐答案

collection_select 不支持选中的选项,其实不需要.它会自动选择其值与表单构建器对象的值匹配的选项.

collection_select doesn't support the selected option, in fact, it doesn't need it. It automatically selects the option whose value matches the value of the form builder object.

让我给你举个例子.假设每个帖子都属于一个类别.

Let me show you an example. Assuming each post belongs to a category.

@post = Post.new

<% form_for @post do |f| %>
  <!-- no option selected -->
  <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true  %>
<% end %>

@post = Post.new(:category_id => 5)

<% form_for @post do |f| %>
  <!-- option with id == 5 is selected -->
  <%= f.collection_select :category_id, Category.all, :id, :name, :prompt => true  %>
<% end %>

编辑:

我建议使用具有代表性的变量名称.使用@categories 而不是@category.:)此外,从只读视图中拆分更新逻辑.

I'd suggest to use representative variable names. Use @categories instead of @category. :) Also, split the update logic from the read-only view.

def categories #Step 2
  @listing = Listing.find(params[:listing_id])
  @seller = Seller.find(@listing.seller_id)
  @categories = Category.find(:all)
  @listing.complete = "step1"

  respond_to do |format|
    if @listing.update_attributes(params[:listing])
      flash[:notice] = 'Step one succesful. Item saved.'
      format.html #categories.html.erb
    end
  end
end

<% form_for @listing do |f| %>
  <%= f.collection_select :category_id, @categories, :id, :name, :prompt => true %>
<% end %>

如果它不起作用(即选择提示),则意味着您没有与该记录关联的 category_id 或 Category 集合为空.在将对象传递给表单之前,请确保不要在某处重置@listing 的 category_id 值.

If it doesn't work (that is it selects the prompt) it means either you don't have a category_id associated to that record or the Category collection is empty. Be sure to not reset the value of category_id for @listing somewhere before the object is passed to the form.

编辑 2:

class Category
  def id_as_string
    id.to_s
  end
end

<%= f.collection_select :category_id, Category.all, :id_as_string, :name, :prompt => true  %>

这篇关于Ruby on Rails Collection 选择 - 如何预先选择正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆