Rails 3 App中的简单搜索表单不显示 [英] Simple Search Form in Rails 3 App not Displaying

查看:92
本文介绍了Rails 3 App中的简单搜索表单不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了37号栏杆,似乎什至无法让我的搜索表单显示在索引页上.有什么想法我的代码有什么问题吗?提前致谢!任何帮助都将不胜感激.

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 %>

在此处使用控制器:

class UsersController < ApplicationController

   ...

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

   ...

最后,user.rb文件:

Lastly, the user.rb file:

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

推荐答案

我看到了您之前的问题.当我提交答案时,它已被删除.这看起来类似于该问题,所以这是我对上一个问题和这个问题的答案.

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

将此字符串添加到用户模型中的attr_accessible中,如下所示:

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使您可以进行批量分配.不要忘记在此处添加新列.

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

完成此操作后,您就可以编写控制器功能了.我将其命名为network是因为我想显示基于国家/地区的用户.我的搜索词将是国家/地区,我将显示属于一个特定国家/地区的所有用户.函数定义如下:

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.

现在显示.我想在views/users/下创建一个新页面,名称为network.html.erb,因为我想显示属于一个国家的用户网络.首先从您要提供输入的地方(即您在其中调用搜索的地方)获取一种形式.对于我来说,我的表格标题中有一个链接.链接如下:

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>

到目前为止,太好了.现在,最后一步是通过将它们添加到routes.rb文件中来连接它们.在该文件中添加以下内容: MyApp :: Application.routes.draw做

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.

这篇关于Rails 3 App中的简单搜索表单不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆