我試圖做一些看起來簡單的概念,但我不能得到它的工作。以下是我想要做的:如何在Rails中提交此表單,以便登錄/ users /:id?
我在/ users/index頁面上有一個簡單的「搜索」表單。它 利用jQuery Tokeninput在當前用戶鍵入搜索字段時自動完成用戶(名稱/用戶名) 。我想要做的 是讓用戶輸入一個名字,從列表中選擇一個用戶,然後點擊 「提交」並被帶到所選用戶的個人資料(/ users /:id/- 這是 用戶#顯示) 。我有jQuery Tokeinput配置爲提交user_id爲:user_token。
我似乎無法得到這個工作。自動完成部分工作正常,但我不知道如何「提交」,以便輸入用戶的配置文件顯示。
這裏的時候,我打表單上的「提交」按鈕(在終端從開發日誌拉)會發生什麼:
Started PUT "https://stackoverflow.com/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點擊提交按鈕)。
在屏幕上,我看到:
所示向上頂的路徑爲... /用戶/ 2(當前用戶的ID),並在 瀏覽器我看到:
NoMethodError UsersController#update
未定義方法`downcase!'爲零:NilClass
我得到的,因爲它試圖在用戶控制器中運行「更新」操作,並有一個「downcase!」在更新操作開始時調用其中一個參數。該參數([: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