select_tag 多个和部分

发布于 2024-09-11 23:55:22 字数 1079 浏览 2 评论 0原文

我遇到了一些讨厌的 select_tags 问题,我可以让它们在新标签上很好地显示数据,但在编辑时,它们不会显示,这是我目前拥有的代码。我想我需要使用 include? 但我不知道把它放在哪里。

这就是我目前所拥有的新内容

<% @data = Code.find(:all, :conditions => "section = 'Location'") %>
<%= select_tag "candidate[code_ids][]", options_for_select(@data.map{|d| ["#{d.value}",d.id]}), :multiple => true %> 

,这是用于编辑的

<% @data = Code.find(:all, :conditions => "section = 'Location'") %>
<%= select_tag "candidate[code_ids][]", options_for_select(@data.map{|d| ["#{d.value}",d.id]},@found), :multiple => true %>         

在我的控制器中,我有这个内容,它可以获取与该候选者相关的所有代码。

@results = Candidate.all :joins => 
    "LEFT JOIN candidates_codes ON candidates.id = candidates_codes.candidate_id",
    :conditions => ["candidates.id = ?", params[:id]],
    :select => "candidates_codes.code_id"
#create an array of the candidates selected codes
@found = []
for c in @results
 @found.push(c.code_id.to_i)
end

我怎样才能做到只有一项选择,而不是一项用于新建,一项用于编辑?

I'm having a problem with some pesky select_tags, I can get them to show data fine on the new but when it comes to edit, they don't show up, here is the code I have at the moment. I think I need to use include? but I don't know where to put it.

This is what I have at the moment for the new

<% @data = Code.find(:all, :conditions => "section = 'Location'") %>
<%= select_tag "candidate[code_ids][]", options_for_select(@data.map{|d| ["#{d.value}",d.id]}), :multiple => true %> 

And this is for the edit

<% @data = Code.find(:all, :conditions => "section = 'Location'") %>
<%= select_tag "candidate[code_ids][]", options_for_select(@data.map{|d| ["#{d.value}",d.id]},@found), :multiple => true %>         

In my controller I have this which grabs all of the codes that are related to that candidate.

@results = Candidate.all :joins => 
    "LEFT JOIN candidates_codes ON candidates.id = candidates_codes.candidate_id",
    :conditions => ["candidates.id = ?", params[:id]],
    :select => "candidates_codes.code_id"
#create an array of the candidates selected codes
@found = []
for c in @results
 @found.push(c.code_id.to_i)
end

How can I make it so I only have one select rather than having one for new and one for edit?

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

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

发布评论

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

评论(1

半枫 2024-09-18 23:55:22

在你问很久之后......但这就是我每次都能正确制作具有多选下拉重新加载的表单的方式。不可否认,这是使用参数而不是数据......

<%= type_list = EventConstants::EventType::to_array 
    select_tag(:event_types, options_for_select(type_list, params[:event_types]), 
               :multiple => true, :size => 5) %>

由于我使用的是 mongo_mapper,我可以像这样链接 Plucky 查询:

qry_where = qry_where.where(:event_type.in => params[:event_types]) if params[:event_types] && !params[:event_types].include?("All")

Long after you asked... But this is how I have been able to make a form with multi-select drop-down reload properly each time. Admittedly, this is using params and not data...

<%= type_list = EventConstants::EventType::to_array 
    select_tag(:event_types, options_for_select(type_list, params[:event_types]), 
               :multiple => true, :size => 5) %>

Since I am using mongo_mapper, I can just chain Plucky queries as so:

qry_where = qry_where.where(:event_type.in => params[:event_types]) if params[:event_types] && !params[:event_types].include?("All")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文