Simple Search Form in Rails 3 App not Displaying

六月ゝ 毕业季﹏ 提交于 2020-01-14 03:49:07

问题


I ran through railscast #37 and can't seem to even get my search form to display on my index page. Any ideas whats wrong with my code? thanks in advance! any help is greatly appreciated.

Here is the index page where the form is located:

<h1>All users</h1>

<% form_tag users_path, :method => 'get' do  %>
  <p>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>

Heres the controller:

class UsersController < ApplicationController

   ...

  def index
     @title = "All users"
     @users = User.paginate(:page => params[:page])   
     @usersearch = User.search(params[:search])
  end

   ...

Lastly, the user.rb file:

...
def self.search(search)
  if search
  where('name LIKE ?', "%#{search}%")
  else
  all
  end
end

回答1:


I saw your previous question. It was deleted while I was submitting my answer. This looks similar to that question, so here is my answer to the previous question and this one.

I searched the users based on their country. So this is what I did for that: First I created a new column country based on what I'm going to search. The command is

  $ rails generate migration add_country_to_users country:string
  $ rake db:migrate

Add this string to the attr_accessible in the user model as follows:

class User < ActiveRecord::Base
   attr_accessor   :password

   attr_accessible :name, :email, :password, :password_confirmation, :country
   validates_presence_of :country

   ..........

attr_accessible lets you do a mass assignment. Don't forget to add the new column here.

Once this is done you are now ready to write a controller function. I named mine network because I want to display users based on country. My search word is going to be country and I will display all the users belonging to one particular country. The function definition is as follows:

class UsersController < ApplicationController
   def network
       @title = "All users"
       @users = User.find_all_by_country(User.find(params[:id]).country)
   end
end

For your question if you want to display users by name make it

User.find_all_by_name(User.find(params[:id]).name) 

or something like that based on our search word.

Now coming to the display. I created a new page under views/users/ as network.html.erb as I want to display a network of users belonging to a country. First have one form from where you will give the input, i.e where you invoke the search. For mine I have a link in the header of my form. The link is as follows:

<li><%= link_to "Network", network_user_path(current_user) %></li>

Once user clicks this the following form will be displayed:

<h1>All users</h1>
<ul class="users"> 
  <% @users.each do |user| %>
  <li>
  <%= gravatar_for user, :size => 30 %>
  <%= link_to user.name, user %> 
  </li>
<% end %> 
</ul>

So far, so good. Now the final step is to connect them by adding them in the routes.rb file. Add the following in that file: MyApp::Application.routes.draw do

  resources :users do
    member do
      get :following, :followers, :network, :news
    end
  end
end

So this is what I did for my application and it worked. I hope it helps.




回答2:


I didn't look further, but there must be an equal sign in form_tag in the recent Rails versions. You should also get an error message on that in development mode. Try <%= form_tag users_path, :method => 'get' do %>



来源:https://stackoverflow.com/questions/9151655/simple-search-form-in-rails-3-app-not-displaying

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!