【问题标题】:Need help with implementing Railscast 198 in Rails 3在 Rails 3 中实现 Railscast 198 需要帮助
【发布时间】: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


【解决方案1】:

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

如果您是 Rails 新手,最好直接复制它,并调整为使用用户而不是产品。然后通过它尝试了解它的作用。

【讨论】:

  • 感谢您的提示。笔记。我将返回并使其对 railscast 起作用,然后重新实现 index 操作和其他项目。
猜你喜欢
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多