Ruby on rails 带有基本搜索栏的未初始化常量[ControllerName]
我正在尝试将搜索添加到我的站点,当我在中键入内容时,会出现路由错误。搜索栏位于导航栏中。我想是控制器的问题吧 错误 控制器Ruby on rails 带有基本搜索栏的未初始化常量[ControllerName],ruby-on-rails,ruby-on-rails-4,search,Ruby On Rails,Ruby On Rails 4,Search,我正在尝试将搜索添加到我的站点,当我在中键入内容时,会出现路由错误。搜索栏位于导航栏中。我想是控制器的问题吧 错误 控制器 class ProfilesController < ApplicationController before_action :set_profile, only: [:show, :edit, :update, :destroy] def index @profiles = Profile.all end def show
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
两者之间存在差异
ProfileController
及
由于您的路由指向“profile”控制器,请在该类名声明中删除“s”。可能还有文件名
或者你也可以改变你的路线
get '/search(.:format)' => 'profiles#show'
两者之间存在差异
ProfileController
及
由于您的路由指向“profile”控制器,请在该类名声明中删除“s”。可能还有文件名
或者你也可以改变你的路线
get '/search(.:format)' => 'profiles#show'
问题在于你的路线没有使用复数约定作为控制器 把它改成
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
方法仅显示特定的记录,而这正是它真正的用途。问题在于您的路线不使用控制器的复数约定
把它改成
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
方法仅显示一条特定的记录,这正是它真正的目的。要添加到@SteveTurcyzn
的答案中,您最好在路线中使用以下内容:
#config/routes.rb
resources :profiles, path: "search", only: [:index] #-> url.com/search
然后,您将能够使用:
#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def index
# Use steve's answer here
end
end
#app/controllers/profiles_controller.rb
类ProfilesController
要添加到@SteveTurcyzn
的答案中,您最好在路线中使用以下内容:
#config/routes.rb
resources :profiles, path: "search", only: [:index] #-> url.com/search
然后,您将能够使用:
#app/controllers/profiles_controller.rb
class ProfilesController < ApplicationController
before_action :set_profile, only: [:show, :edit, :update, :destroy]
def index
# Use steve's answer here
end
end
#app/controllers/profiles_controller.rb
类ProfilesController
更新了我的答案,告诉我@rs19Thank在哪里。所以当我这么做的时候,看看我对下面答案的评论。它调出一个新的错误,那是另一个错误。这个答案让你克服了你的错误,不过我希望你能投赞成票:)Ps你的新错误只是说你找不到没有id的记录。根据你的路线,你会在你的url中这样称呼它。。。“/search?id=123”更新了我的答案,告诉我@rs19Thank在哪里。所以当我这么做的时候,看看我对下面答案的评论。它调出一个新的错误,那是另一个错误。这个答案让你克服了你的错误,不过我希望你能投赞成票:)Ps你的新错误只是说你找不到没有id的记录。根据你的路线,你会在你的url中这样称呼它。。。“/search?id=123”因此,当我这样做时,我会得到以下错误:ActiveRecord::RecordNotFound in ProfilesController#show。找不到“id”=
的配置文件,出现的代码是def set_Profile@Profile=Profile.find(params[:id])end
是的,但这是另一个问题。您想对返回的搜索词做什么?它是否在配置文件的列中?可能是配置文件名称
?您希望如何显示配置文件?你最好直接搜索profiles#index
明白了。只是尝试搜索类似于工作的内容,然后返回与之匹配的配置文件。i、 “演员”谢谢。还是不为我工作。但是谢谢你的帮助。我想知道搜索栏是否在application.html而不是index.htmlso中是否重要。因此,当我这样做时,会出现以下错误:ActiveRecord::RecordNotFound in ProfilesController#show。找不到“id”=
的配置文件,出现的代码是def set_Profile@Profile=Profile.find(params[:id])end
是的,但这是另一个问题。您想对返回的搜索词做什么?它是否在配置文件的列中?可能是配置文件名称
?您希望如何显示配置文件?你最好直接搜索profiles#index
明白了。只是尝试搜索类似于工作的内容,然后返回与之匹配的配置文件。i、 “演员”谢谢。还是不为我工作。但是谢谢你的帮助。我想知道搜索栏在application.html而不是index.htmltanks中是否重要。知道现在如何在视图中呈现这些结果的好资源是什么吗?如果你给我一些上下文,我可以告诉你怎么做。您正在为搜索
呈现索引
-是否要对配置文件
进行搜索?谢谢。知道现在如何在视图中呈现这些结果的好资源是什么吗?如果你给我一些上下文,我可以告诉你怎么做。您正在为搜索呈现索引
-是否要运行配置文件的搜索?