我一直在尝试实现Ryan Bates Railscast 198到rails 3像4天…至少在晚上,如果你明白我的意思。无论如何,这是我现在的代码:
用户控制器动作(设计设置以显示已批准的不同状态):
def index
if params[:approved] == "false"
@users = User.find_all_by_approved(false)
elsif
@users = User.find_all_by_approved(true)
else
@users = User.all
end
end
def update
@user = User.find(params[:id])
respond_to do |format|
if @user.update_attributes(params[:user])
format.html { redirect_to root_path flash[:notice] = "User was successfully updated." }
else
format.html { render :action => "edit" }
end
end
end
def edit_individual
@users = User.find(params[:user_ids])
end
def update_individual
@users = User.update(params[:users].keys, params[:users].values).reject { |p| p.errors.empty? }
if @users.empty?
flash[:notice] = "Users updated"
redirect_to users_url
else
render :action => "edit_individual"
end
end
end
我的用户#指数
<h1> Users</h1>
<%= link_to "All Users", :action => "index" %> |
<%= link_to "Users awaiting approval", :action => "index", :approved => "false"%>
|
<%= link_to "Approved Users", :action => "index", :approved => "true" %>
<%= form_tag edit_individual_users_path, :method => :put do %>
<table>
<tr>
<th>Email Address</th>
<th>Approved</th>
<th></th>
</tr>
<% for user in @users %>
<tr>
<td> <%= user.email %></td>
<td><%= check_box_tag user.id %></td>
<td> <%= link_to "Edit", edit_user_path(user) %></td>
</tr>
<% end %>
<p><%= submit_tag "Edit Checked" %></p>
</table>
<% end %>
和用户#edit_individual
<%= form_tag update_individual_users_path, :method => :put do %>
<% for user in @users %>
<%= fields_for "users[]", user do |f| %>
<h2><%=h user.name %></h2>
<p><%= check_box_tag user.id %></p>
<% end %>
<% end %>
<p><%= submit_tag "Submit" %></p>
<% end %>
routes.rb
devise_for :users
resources :users do
collection do
post :edit_individual
put :update_individual
end
end
end
所以我通过谷歌来处理基本问题:fields_for
需要一个"="之类的东西。#index显示良好,但如果我选中一个复选框,然后点击edit checked按钮,我会得到以下错误:
ActiveRecord::RecordNotFound in UsersController#update
Couldn't find User with id=edit_individual
有什么想法?Thanks so much
请检查您的路线。rb和控制器代码,因为它与Railscast 198页面上的代码相差很大:http://railscasts.com/episodes/198-edit-multiple-individually
如果你刚开始使用rails,最好是复制它,并调整为使用用户而不是产品。然后通读一遍,试着理解它的作用