Ruby Rails 表单未捕获 select 多个字段中的值
Ruby on Rails form isn't capturing values in select multiple field
我按照此处的示例为我的表单创建了一个 select 多元素:Ruby on Rails -- multiple selection in f.select
但是,当单击提交按钮时,不会捕获 select 字段中的值。使用复选框时我遇到了同样的问题。不过,当我将其更改为文本字段时它会起作用。我错过了一些明显的东西吗?如何从 select 字段中捕获用户 select 的值?我的代码如下。
形式:new.html.erb
<%= form_for @service do |f| %>
<div class="row">
<h3>Operating hours</h3>
<p>Please select the days you are open. Hold CTRL (Windows) or Command (Mac) to select multiple.</p>
<div class="form-group">
<div class="col-md-6">
<%= f.select(:work_days, [['Monday', 'Monday'],
['Tuesday', 'Tuesday'],
['Wednesday', 'Wednesday'],
['Thursday', 'Thursday'],
['Friday', 'Friday'],
['Saturday', 'Saturday'],
['Sunday', 'Sunday']
],{},
{ :multiple => true, :size => 7 }
) %>
</div> <!-- .col-md-6 -->
</div> <!-- .form-group -->
</div> <!-- .row -->
<!-- the code in the div below works -->
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="work_days">work days</label>
<%= f.text_field :work_days, placeholder: "Type the days you are available", class: "form-control", required: true, id: "work_days" %>
</div>
</div>
</div> <!-- .row -->
<div class="text-center">
<%= f.submit "Save", class: "btn btn-success" %>
</div>
<% end %>
相关控制器代码:services_controller.rb
class ServicesController < ApplicationController
# require the user to be logged in to edit and create, but not to view
before_action :set_service, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
before_action :is_authorised, only: [:listing, :update]
def new
@service = current_user.services.build
end
def create
@service = current_user.services.build(service_params)
if @service.save
redirect_to listing_service_path(@service), notice: "Saved..."
else
flash[:alert] = "Something went wrong..."
render :new
end
end
private
def set_service
@service = Service.find(params[:id])
end
def service_params
params.require(:service).permit(:title, :description, :work_days, :open_time, :close_time)
end
end
在 service_params
中,work_days
应该接受一个数组,因此它将接受多个值:
def service_params
params.require(:service).permit(
:title,
:description,
:open_time,
:close_time,
work_days: []
)
end
我按照此处的示例为我的表单创建了一个 select 多元素:Ruby on Rails -- multiple selection in f.select
但是,当单击提交按钮时,不会捕获 select 字段中的值。使用复选框时我遇到了同样的问题。不过,当我将其更改为文本字段时它会起作用。我错过了一些明显的东西吗?如何从 select 字段中捕获用户 select 的值?我的代码如下。
形式:new.html.erb
<%= form_for @service do |f| %>
<div class="row">
<h3>Operating hours</h3>
<p>Please select the days you are open. Hold CTRL (Windows) or Command (Mac) to select multiple.</p>
<div class="form-group">
<div class="col-md-6">
<%= f.select(:work_days, [['Monday', 'Monday'],
['Tuesday', 'Tuesday'],
['Wednesday', 'Wednesday'],
['Thursday', 'Thursday'],
['Friday', 'Friday'],
['Saturday', 'Saturday'],
['Sunday', 'Sunday']
],{},
{ :multiple => true, :size => 7 }
) %>
</div> <!-- .col-md-6 -->
</div> <!-- .form-group -->
</div> <!-- .row -->
<!-- the code in the div below works -->
<div class="row">
<div class="col-md-8">
<div class="form-group">
<label for="work_days">work days</label>
<%= f.text_field :work_days, placeholder: "Type the days you are available", class: "form-control", required: true, id: "work_days" %>
</div>
</div>
</div> <!-- .row -->
<div class="text-center">
<%= f.submit "Save", class: "btn btn-success" %>
</div>
<% end %>
相关控制器代码:services_controller.rb
class ServicesController < ApplicationController
# require the user to be logged in to edit and create, but not to view
before_action :set_service, except: [:index, :new, :create]
before_action :authenticate_user!, except: [:show]
before_action :is_authorised, only: [:listing, :update]
def new
@service = current_user.services.build
end
def create
@service = current_user.services.build(service_params)
if @service.save
redirect_to listing_service_path(@service), notice: "Saved..."
else
flash[:alert] = "Something went wrong..."
render :new
end
end
private
def set_service
@service = Service.find(params[:id])
end
def service_params
params.require(:service).permit(:title, :description, :work_days, :open_time, :close_time)
end
end
在 service_params
中,work_days
应该接受一个数组,因此它将接受多个值:
def service_params
params.require(:service).permit(
:title,
:description,
:open_time,
:close_time,
work_days: []
)
end