Skip to content Skip to sidebar Skip to footer

Rails:how To Create Dynamic Drop-down Box In Rails?

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

Solution 1:

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)

Post a Comment for "Rails:how To Create Dynamic Drop-down Box In Rails?"