我一直试图将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
我的用户#index
<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>
&lt;%end%&gt;
的routes.rb
devise_for :users
resources :users do
collection do
post :edit_individual
put :update_individual
end
end
end
所以我通过谷歌搜索来处理基本问题:fields_for
需要这样的“=”之类的东西。 #index显示正常,但如果我选中一个复选框然后点击编辑选中按钮,我会收到以下错误:
ActiveRecord::RecordNotFound in UsersController#update
Couldn't find User with id=edit_individual
任何想法???非常感谢
答案 0 :(得分:0)
请检查您的routes.rb和控制器代码,因为它与Railscast 198页面上的代码相差无几:http://railscasts.com/episodes/198-edit-multiple-individually
如果您是初次使用rails并且使用用户而不是产品进行调整,最好将其复制。然后通过它试图了解它的作用。