【问题标题】:rails:how to create dynamic drop-down box in rails?rails:如何在rails中创建动态下拉框?
【发布时间】:2013-11-27 13:03:41
【问题描述】:
class FeedbacksController < ApplicationController
    def new
    @feedback = Feedback.new
    @subject = Subject.find_all_by_teacher_id(current_user.id)
end

end

这里是视图,


  <%= f.label :strengths %>
  <%= f.text_field :strengths %>

  <%= f.label :rating %>
  <%= f.radio_button :rating, 'A', :checked => true %> A
  <%= f.radio_button :rating, 'B' %> B
  <%= f.radio_button :rating, 'C' %> C
  <%= f.radio_button :rating, 'D' %> D



  <%= f.label :recommendations %>
  <%= f.text_field :recommendations %>






  <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>

如果选择了一个主题,我想创建第二个下拉框,系统会从 DB 中搜索拥有该主题的学生。这意味着当我选择另一个主题时,这个下拉框也会改变

【问题讨论】:

  • 不确定我是否在效仿你的榜样。会不会像(在州和城市的背景下) - 如果我选择一个州,下一个下拉框会填充该州内的所有城市?
  • 您可以在动态选择菜单上查看 railscast。这应该为您指明大方向。 railscasts.com/episodes/88-dynamic-select-menus

标签: javascript jquery html css ruby-on-rails


【解决方案1】:

例如,我有一个 Country 表,其中包含 国家,我正在为用户创建一个表单。以我需要的形式 国家名称应该动态来自数据库,我会 在Select

中使用所有名称作为Options

当我使用表单创建新用户时,我会使用:

%label COUNTRY
      =f.select :user_country, options_for_select(Country.order(:country_name).pluck(:country_name))

当我使用表单来编辑现有用户时,我将使用:

%label COUNTRY
  =f.select :user_country, options_for_select(Country.order(:country_name).pluck(:country_name), :selected=>@user.user_country)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多