[英]How to change a label_tag to a drop down in rails app
我正在构建一个Rails应用程序,在那里我的用户属于拥有许多教室的学校。
用户在关联的教室中创建图钉并使用代码来确保他们在正确的教室中将图钉关联。 现在,我正在使用文本字段,但是我需要切换到一个列出所有课堂代码的下拉菜单。 我很难弄清楚这一点。 (是RoR的新功能)。
这是表单的一部分,我需要更改表单以创建图钉:
<div class="form-group">
<%= label_tag(:classroom, "Enter your classroom code:") %>
<input type="text" name="pin[code]">
</div>
教室模式:
class Classroom < ActiveRecord::Base
belongs_to :school
belongs_to :teacher, :class_name => "User"
has_and_belongs_to_many :users
has_many :pins
has_many :reflections
validates_presence_of :school
validates_presence_of :teacher
validates :code, :uniqueness => { :scope => :school_id }
end
引脚型号
class Pin < ActiveRecord::Base
belongs_to :user
belongs_to :classroom
has_and_belongs_to_many :emotions
validates_presence_of :user
validates_presence_of :classroom
end
引脚控制器
class PinsController < ApplicationController
before_action :set_pin, only: [:show, :edit, :update, :destroy]
respond_to :html
def search
index
render :index
authorize @pins
end
def home
@pins = Pin.all
respond_with(@pins)
authorize @pins
end
def show
respond_with(@pin)
end
def new
@pin = Pin.new
@emotions = Emotion.all
@school = School.find(params[:school])
respond_with(@pin)
authorize @pin
end
def edit
end
def create
code = params[:pin][:code]
@classroom = Classroom.where('code LIKE ?', code).first
unless @classroom
flash[:error] = "Classroom code incorrect"
render :new
else
params[:pin][:classroom_id] = @classroom.id
end
@pin = Pin.new(pin_params)
@pin.save
params[:pin][:emotion_ids].each do |emotion_id|
@emotion = Emotion.find(emotion_id)
@pin.emotions << @emotion
end
if @pin.save
redirect_to signout_path and return
end
respond_with(@pin)
authorize @pin
end
def update
@pin.update(pin_params)
respond_with(@pin)
authorize @pin
end
def destroy
@pin.destroy
respond_with(@pin)
authorize @pin
end
end
您可以使用select_tag
帮助器在rails中创建一个下拉选择框。 您可以执行以下操作:
<div class="form-group">
<%= label_tag(:classroom, "Select your classroom code:") %>
<%= select_tag "pin[code]", options_from_collection_for_select(Classroom.all, "code", "code") %>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.