0

我一直在尝试将 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>

<% 结束 %>

路线.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

有任何想法吗???非常感谢

4

1 回答 1

0

请检查您的 routes.rb 和控制器代码,因为它与 Railscast 198 页面上的代码相差很大: http ://railscasts.com/episodes/198-edit-multiple-individually

如果您不熟悉 Rails 并调整以使用用户而不是产品,最好将其复制进去。然后通过它尝试了解它的作用。

于 2011-09-22T04:18:26.353 回答