0

I'm trying to do something that seems conceptually simple, but I just can't get it working. Here's what I'm trying to do:

I have a simple "search" form on the /users/index page. It leverages jQuery Tokeninput to autocomplete a user (name/username) when the current user types into the search field. What I want to do is let the user type a name, select a user from the list, then click "submit" and be taken to the selected user's profile (/users/:id/ - which is Users#show). I have jQuery Tokeinput configured to submit the user_id as :user_token.

I can't seem to get this working. The autocomplete part works correctly, but I can't figure out how to "submit" so that the entered user's profile is shown.

Here's what happens when I hit the "submit" button on the form (pulled from the development log in the terminal):

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'

So it's calling the update action on the users controller, I assume because "@user" already exists (specifically, it's the current_user who clicks the submit button).

On screen, I see:

The path shown up top is .../users/2 (the id of current user), and in the browser I see:

NoMethodError in UsersController#update

undefined method `downcase!' for nil:NilClass

I'm getting that because it's trying to run the "update" action in the Users controller, and there's a "downcase!" call on one of the params at the beginning up the update action. That parameter ([:user][:email]) obviously doesn't exist since it's not in the form I'm submitting.

What I really want to do is go to "/users/41" (the show page for the user whose id is passed as params[:user][:user_token]). How do I do this?

Here's all the relevant code:

#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

My routes...

#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'

Here is my form (this definitely does NOT work, although the jQuery Tokeninput does work):

#_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>

Here's the relevant part of my user model:

#user.rb
attr_accessible  :user_token

attr_reader :user_token
JoshDoody
  • 417
  • 1
  • 4
  • 14

1 Answers1

0
<% form_for @project, :url => { :controller => "project", :action => "thumbnail_save" } do |form| %>
....
<% end %>

Also write something like this in your config file (put it at the top)

get "users/show"

If that doesn't work try this. (you might have to change your form for below to work)

match "project/thumbnail_save", :to => "project#thumbnail_save"

Let us know if any more issues.

Kashyap
  • 479
  • 5
  • 17