我遇到了下面显示的代码问题!我想创建一个显示用户名列表的视图(如user_weight方法中的@participate所示)。在我希望提供下拉列表的名称旁边,您可以在其中为特定用户分配1到5之间的值。
现在我完全卡住了!该视图仅显示我对问题的处理方法根本不起作用...(:与会者&:user_weight现在只是占位符)
如果我尝试它的方式我得到一个:"未定义的方法`merge' [[" 1",1],[" 2",2],[" 3",3],[" 4" ,4],[" 5",5]]:数组"错误
希望有人有个主意!
文件:
user_weight.html.erb
<%= form_for @users, :url => {action: "create_user_weight"} do |f| %>
<ul>
<% @users.each do |user| %>
<li> <%= user.username %> <%= f.select(:attendee, :user_weight, [['1', 1], ['2', 2], ['3', 3], ['4', 4], ['5', 5]]) %> </li>
<% end %>
<p>
<%= button_to "Select User Weight", action: "create" %>
</p>
<% end %>
</ul>
schema.rb
create_table "attendees", force: true do |t|
t.integer "task_id"
t.integer "user_id"
t.boolean "participate"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_weight"
end
create_table "friendships", force: true do |t|
t.integer "user_id"
t.integer "friend_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "selectdates", force: true do |t|
t.datetime "task_date"
t.boolean "participate"
t.integer "attendee_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "task_id"
end
create_table "tasks", force: true do |t|
t.string "title"
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "participate"
t.date "meeting_date"
t.time "meeting_start_time"
t.time "meeting_end_time"
end
create_table "users", force: true do |t|
t.string "email"
t.string "password_hash"
t.string "password_salt"
t.string "username"
t.string "company"
t.datetime "created_at"
t.datetime "updated_at"
end
end
tasks_controller.rb
def select_user
@user = User.all
@task = Task.find(params[:id])
end
def participate
@task = Task.find(params[:id])
@task.users << User.all
@attendees = Attendee.where(User_id: params[:user_checkbox])
Attendee.transaction do
@attendees.each do |attendee|
attendee.update_attributes!(participate: true)
end
end
redirect_to :action => 'user_weight', :id => @task
end
def user_weight
@participants = Attendee.where(participate: true, task_id: params[:id])
@users = @participants.map { |p| User.find_by id: p.user_id }.to_set.to_a
end
attendee.rb
class Attendee < ActiveRecord::Base
belongs_to :task
belongs_to :user
end
user.rb
class User < ActiveRecord::Base
has_many :attendees
has_many :tasks, :through => :attendees
has_many :friendships
has_many :friends, :through => :friendships
has_many :selectdates
end
task.rb
class Task < ActiveRecord::Base
has_many :attendees
has_many :users, :through => :attendees
has_many :selectdates, dependent: :destroy
end
的routes.rb
Rails.application.routes.draw do
#match ':controller(/:action(/:id))(.:format)', via: [:get, :post]
get 'tasks/new'
get 'tasks/create'
get 'tasks/select_date'
get 'tasks/user_weight', :as => "user_weight"
get 'tasks/select_user', :as => "select_user"
match "tasks/participate" => "tasks#participate", :via => :post
match "tasks/create_date" => "tasks#create_date", :via => :patch, :as => "create_date"
match "tasks/create_user_weight" => "tasks#create_user_weight", :via => :patch, :as => "create_user_weight"
resources :tasks
get "logout" => "sessions#destroy", :as => "logout"
get "login" => "sessions#new", :as => "login"
get "signup" => "users#new", :as => "signup"
get "account" => "sessions#account", :as => "account"
get "addfriends" => "friendships#new", :as => "addfriends"
resources :users do
collection do
update 'multiple_update'
end
end
resources :sessions
resources :friendships
root 'welcome#index'
答案 0 :(得分:1)
form_for不知道如何正确处理一组用户。我会为此使用form_tag。您需要在用户控制器中执行自定义操作。我建议像这样设置:
#routes
resources :users do
collection do
update 'multiple_update'
end
end
#controller
#expects params like {:users => {123 => {:weight => 3}, 456 => {:weight => 4}}
#where 123 and 456 are user ids
def multiple_update
@users = []
params[:users].each do |id, attributes|
if user = User.find_by_id(id)
if user.update_attributes(attributes)
@users << user
end
end
end
end
#view
<% form_tag multiple_update_users_path, :method => :update do %>
<ul>
<% @users.each do |user| %>
<li> <%= user.username %> <%= select_tag "users[#{user.id}][weight]", options_for_select([['1', 1], ['2', 2], ['3', 3], ['4', 4], ['5', 5]], user.weight) %> </li>
<% end %>
</ul>
<%= submit_tag "Update" %>
<% end %>