rails:如何在rails中创建动态下拉框? [英] rails:how to create dynamic drop-down box in rails?

查看:103
本文介绍了rails:如何在rails中创建动态下拉框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class FeedbacksController< ApplicationController 
def new
@feedback = Feedback.new
@subject = Subject.find_all_by_teacher_id(current_user.id)
end

end

这里是视图,



<%= form_for @feedback)do | f | %>
<%= render'shared / error_messages',object:f.object%>

<%= f.label:subject_id%>

<%= f.collection_select(:subject_id,@subject,:id,:name)%>

  f.label:strength%> 
<%= f.text_field:highlights%>

<%= f.label:rating%>
<%= f.radio_button:rating,'A',:checked =>真实%> 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创建我的帐户,class:btn btn-large btn- primary%>
<%end%>



我想创建第二个下拉框主题,系统将从DB.That的意思,当我选择另一个主题,这个下拉框也将更改

解决方案


例如,我有一个国家/地区表,其中包含
个国家/地区的名称,我正在为用户创建一个表单。在我需要所有
国家名称的形式应来自数据库动态,我将
使用所有名称选项选择


当我在表单中创建新用户时,我将使用:

 %label COUNTRY 
= f.select:user_country,options_for_select(Country.order(:country_name).pluck(:country_name))
当我在表单中使用表单编辑现有用户时,我将使用

p>

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


class FeedbacksController < ApplicationController
    def new
    @feedback = Feedback.new
    @subject = Subject.find_all_by_teacher_id(current_user.id)
end

end

Here is view,
<%= form_for(@feedback) do |f| %> <%= render 'shared/error_messages', object: f.object %>
<%= f.label :subject_id %>
<%= f.collection_select(:subject_id, @subject, :id, :name ) %>

  <%= 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 %>

I wanna to create a second drop-down box if chose a subject ,system will search students who have this subject from DB.That meaning when I choose another subject this drop-down box will also change

解决方案

Say for example I have a Country table which contains names of countries, and I am creating a form for user. In the form I need all countries name should come dynamically from database, and I will use all names as Options in Select

When I am using in form for creating a new user, I will use:

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

When I am using in form for editing an existing user, I will use:

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

这篇关于rails:如何在rails中创建动态下拉框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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