带有基本搜索栏的未初始化常量[ControllerName]

时间:2015-12-24 23:25:35

标签: ruby-on-rails ruby-on-rails-4 search

我正在尝试将搜索添加到我的网站,当我输入内容时,我收到路由错误。搜索栏位于导航栏中。我认为这是控制器的东西?

ERROR

Routing Error
uninitialized constant ProfileController

控制器

class ProfilesController < ApplicationController
  before_action :set_profile, only: [:show, :edit, :update, :destroy]

  def index
    @profiles = Profile.all
  end

  def show
          @profiles = Profile.find(params[:id])
  end

HTML

        <nav>
          <ul class="nav navbar-nav navbar-right">
            <li id="logo"><%= link_to "Pallet",   '/' %></li>
            <li><%= link_to "Home",   '/' %></li>
            <li><%= link_to "Help",   '#' %></li>
            <li><%= link_to "Log in", '#' %></li>
           <li> <%= link_to 'New Profile', new_profile_path %></li>
            <li> <%= link_to 'Search', profiles_path %>           
        <%= form_tag("/search", method: "get") do %>


 <%= label_tag(:q, "Search for:") %>
  <%= text_field_tag(:q) %>
  <%= submit_tag("Search") %>
<% end %>
              </ul>
            </nav>

路线

get '/search(.:format)' => 'profile#show'

3 个答案:

答案 0 :(得分:2)

问题在于您的路线中没有使用复数约定的控制器。

将其更改为...

get '/search(.:format)' => 'profiles#show'

更好的是,做......

get '/search(.:format)' => 'profiles#index'

制作索引方法......

def index
  if params[:q]
    @profiles = Profile.where('name LIKE ?', "%#{params[:q]}%")
  else
    @profiles = Profile.all
  end
end

然后,您只需使用show方法来显示特定记录,这就是它的真正用途。

答案 1 :(得分:1)

之间存在差异
ProfileController

并且

ProfilesController

由于您的路线转到&#39;个人资料&#39;控制器放弃了&#39;在该类名声明中。也可能是文件名

或者您可以改变您的路线

get '/search(.:format)' => 'profiles#show'

答案 2 :(得分:1)

要添加到chmod 644 salonadmin.php的答案,您最好在@SteveTurcyzn中使用以下内容:

routes

然后您就可以使用:

#config/routes.rb
resources :profiles, path: "search", only: [:index] #-> url.com/search