我正在尝试做一些在概念上看起来很简单的事情,但我就是无法让它发挥作用。这是我正在尝试做的事情:
我在 /users/index 页面上有一个简单的“搜索”表单。当当前用户输入搜索字段时,它利用 jQuery Tokeninput 自动完成用户(名称/用户名)。我想要做的是让用户输入一个名称,从列表中选择一个用户,然后单击“提交”并转到所选用户的个人资料(/users/:id/ - 这是用户#show)。我已将 jQuery Tokeinput 配置为将 user_id 提交为:user_token。
我似乎无法让这个工作。自动完成部分工作正常,但我不知道如何“提交”以便显示输入的用户个人资料。
当我点击表单上的“提交”按钮(从终端的开发日志中提取)时,会发生以下情况:
Started PUT "/users/2" for 127.0.0.1 at 2012-05-08 11:19:56 -0400
Processing by UsersController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"blah blah=", "user"=>{"user_token"=>"41"}, "commit"=>"Go to profile", "id"=>"2"}
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = 2 LIMIT 1
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", "2"]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1
Completed 500 Internal Server Error in 83ms
NoMethodError (undefined method `downcase!' for nil:NilClass):
app/controllers/users_controller.rb:119:in `update'
所以它在用户控制器上调用更新操作,我假设因为“@user”已经存在(具体来说,它是 current_user 单击提交按钮)。
在屏幕上,我看到:
顶部显示的路径是 .../users/2 (当前用户的 id),在浏览器中我看到:
用户控制器中的 NoMethodError#update
未定义的方法“小写!” 对于零:NilClass
我得到这个是因为它试图在用户控制器中运行“更新”操作,并且有一个“小写!” 在更新操作开始时调用其中一个参数。该参数 ([:user][:email]) 显然不存在,因为它不在我提交的表单中。
我真正想做的是转到“/users/41”(其 id 作为 params[:user][:user_token] 传递的用户的显示页面)。我该怎么做呢?
以下是所有相关代码:
#users_controller.rb#Index
def index
@title = "All users"
@label = "All users"
@list_users = User.order(:name).page(params[:page]) #generates users shown on index page
@user = current_user
# This is used to populate the autocomplete field in the little search form
@users = User.where("LOWER(name) like ? OR LOWER(username) like ?", "%#{params[:q].downcase}%", "%#{params[:q].downcase}%").order('name ASC').limit(10) if params[:q]
respond_to do |format|
format.html # index.html.erb
format.json { render json: @users, :only => [:id, :name, :username] }
end
end
我的路线...
#routes.rb
resources :comments
resources :invitations
resources :sessions, :only => [:new, :create, :destroy]
resources :shares, :controller => "item_shares", :as => "item_shares" do
resources :comments
end
resources :posts, :controller => "item_posts", :as => "item_posts" do
resources :comments
end
resources :items
resources :relationships, only: [:create, :destroy]
resources :users do
member do
get :following, :followers
end
end
resources :password_resets
match '/signup', :to => 'users#new'
match '/signup/:invitation_token' => 'users#new', :as => :signup_with_invitation
match '/signin', :to => 'sessions#new'
match '/signout', :to => 'sessions#destroy'
match '/invite', :to => 'invitations#new'
match '/users/:id/shared', :to => 'users#shared'
match '/users/:id/received', :to => 'users#received'
match '/users/:id/saved', :to => 'users#saved'
match '/users/:id/posts', :to => 'users#posts'
match '/reciprocal_followers', :to => 'users#reciprocal_followers'
root :to => 'pages#home'
这是我的表单(这绝对行不通,尽管 jQuery Tokeninput 确实有效):
#_user_search_form.html.erb
<div class="form">
<span class="form-label-right round-bottom-left-5 round-top-right-5 gray-gradient">Find someone</span>
<%= form_for @user, :action => "show" do |f| %>
<div class="field">
<%= f.label :user_token, "Name or Username" %></br>
<%= f.text_field :user_token, :placeholder => 'John Doe or JohnDoe123', "data-pre" => (@pre_populate_data.to_json(:only => [:id, :name, :username]) unless @pre_populate_data.nil?) %>
</div>
<div class="actions">
<%= f.submit "Go to profile" %>
</div>
<% end %>
</div>
这是我的用户模型的相关部分:
#user.rb
attr_accessible :user_token
attr_reader :user_token