我一直試圖將Ryan Bates Railscast 198應用到rails 3中,就像4天一樣......至少在晚上,如果你知道我的意思。總之,這裏是我的代碼現在所擁有的:需要在Rails 3中實現Railscast 198的幫助
用戶控制器操作(制定設置,顯示批准的不同狀態):
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>
<%端%>
的routes.rb
devise_for :users
resources :users do
collection do
post :edit_individual
put :update_individual
end
end
end
所以我處理的基本通過在google:fields_for
需要一個 「=」 這樣的東西。 #INDEX顯示正常,但如果我選中一個複選框,然後點擊編輯按鈕檢查我得到以下錯誤:
ActiveRecord::RecordNotFound in UsersController#update
Couldn't find User with id=edit_individual
任何想法?非常感謝
我有點迷失在你的索引方法的條件語句中;我幾乎是積極的,它沒有做你認爲的事情。看看HTML如何實際呈現,特別是表單字段名稱,並確保它符合您的期望。 –