【发布时间】:2011-09-22 03:53:00
【问题描述】:
我一直在尝试将 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>
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
有什么想法吗???非常感谢
【问题讨论】:
-
我有点迷失在你的
index方法的条件语句中;我几乎可以肯定它并没有像你想象的那样做。查看 HTML 的实际呈现方式,尤其是表单字段名称,并确保它符合您的期望。
标签: ruby-on-rails ruby-on-rails-3 railscasts