我有一个表单,不会保存用户通过复选框选择的信息。我猜我写的form_for不正确。我有一个用户模型,它将复选框列表存储在一个数组中。我非常感谢有关如何保存用户在复选框中输入的信息的帮助。我的设置如下:
_form.html
<%= form_for @user, html: {multipart:true} do |f| %>
<%= render "shared/error_messages", object: f.object %>
<label for="user-amenities" class="top">Amenities</label>
<ul class="group" id="user-amenities">
<% User.amenities_list.each_with_index do |amenity, index| %>
<li class="checkbox-li">
<input type="checkbox" name="user_amenities_indicies[]" value="<%= index %>">
<%= amenity %>
</input>
</li>
</ul>
<% end %>
user.rb
require 'elasticsearch/model'
class User < ActiveRecord::Base
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
authenticates_with_sorcery!
validate :amenities
def self.numerical_options
["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16+"]
end
def self.amenities_list
["Smoking Allowed",
"Pets Allowed",
"TV",
"Cable TV",
"Internet",
"Wireless Internet",
"Air Conditioning",
"Heating",
"Elevator in Building",
"Handicap Accessible",
"Pool",
"Kitchen",
"Free parking on premise",
"Doorman",
"Gym",
"Hot Tub",
"Indoor Fireplace",
"Buzzer/Wireless Intercom",
"Breakfast",
"Family/Kid Friendly",
"Suitable for Events",
"Washer",
"Dryer"]
end
def self.integer_from_options_list(options_list)
# convert options list given by radio buttons into one-hot integer
amenities = 0;
if options_list
options_list.each do |option|
amenities += 2 ** option.to_i
end
end
amenities
end
def self.find_with_filters(filters)
filtered_users = User
if filters[:amenities]
amenities = User.integer_from_options_list(filters[:amenities])
filtered_users = filtered_users.where("amenities & ? = ?", amenities, amenities)
end
end
def set_amenities_from_options_list!(options_list)
self.amenities = User.integer_from_options_list(options_list)
end
def boolean_array_from_amenities_integer
[].tap do |amenities_list|
User.amenities_list.length.times do |order|
amenities_list << (self.amenities & 2 ** order > 0)
end
end
end
end
users_controller.rb
def create
@user = User.new(user_params)
@user.set_amenities_from_options_list!(params[:user_amenities_indicies])
if @user.save
flash[:success] = "Thank you for signing up! A confirmation email has been sent to your inbox"
redirect_to users_path
else
render :new
end
end
视图/用户/索引/ _user_list.html.erb
<ul class="group" id="users-index-list">
<% @users ||= users %>
<script type="application/json" id="bootstrap">
<%= @users.to_json.html_safe %>
</script>
<%= render partial:"users/index/user", collection: @users %>
<li><%= paginate @users %></li>
</ul>
视图/用户/索引/ _user.html.erb
<li class="group list-item">
<a href=<%= user_url(user.id) %> class="user-image-wrapper">
<%= image_tag(user.image_url(:thumb), class: "user-image user-image-small") %>
<span class="map-number"><%= user_counter + 1 %></span>
</a>
<%= link_to image_tag(user.image_url(:thumb), class: "host-image user-image-thumb"), user_url(user.id), method: :get %>
<header>
<h3>
<%= link_to user.first_name, user, method: :get%>
</h3>
</header>
在控制台中我得到:
"user_amenities_indicies"=>["0", "1", "2"], "commit"=>"Update User", "id"=>"3"}
Connection refused - connect(2)