选择带有文本覆盖的框

时间:2016-09-08 16:43:26

标签: ruby-on-rails ruby

我有一个简单的表格,内容如下:

<%= select_tag(:location_select, options_for_select(@locations)) %>
<%= text_field_tag(:location_text) %>

如果指定的话,控制器中接受选择框提供的值的最简洁方法是什么,否则返回文本框?

如果两者都被选中,则选择框应该优先。

1 个答案:

答案 0 :(得分:1)

最简洁的方法是在模型中创建验证,然后在控制器中创建类似的验证:

@location = Location.new(locations_params)

if @location.save # if your model accept params of new object
  redirect_to something_path # if you have action index in locations controller wil be: redirect_to locations_path
else
  render 'your_form_name' # if you have your form in 'new' action will be render 'new'
end

模型中验证的示例:

class Location < ApplicationRecord
  validates :something_field, presence: true
end

more about validates