在使用嵌套资源路由时尝试查看搜索结果时,我一直收到以下错误消息,如下所示。出于某种原因,我无法使用以下路线并使用控制器中的redirect_to @search
调用来显示搜索结果。
的routes.rb :
resources :users do
resources :searches
end
以下是错误:
NoMethodError in SearchesController#create
undefined method `search_url' for #<SearchesController:0x007fc68a881708>
Extracted source (around line #19):
17 @search = current_user.searches.create(search_params)
18
19 redirect_to @search
20
21 end
22
搜索控制器:
class SearchesController < ApplicationController
before_action :require_user
def index
end
def new
# @states = ["red","green","blue"]
@states = State.all
@cities = City.all
@languages = Language.all
@search = Search.new
end
def create
@search = current_user.searches.create(search_params)
redirect_to @search
#Old Search
#@search = Search.create(search_params)
end
def show
#@search = Search.find(params[:id])
#@search = @user.searches.find(params[:id])
@search = current_user.searches.find_by(id: params[:id])
end
#Deleting searches, tied to the "delete link" on the view
def destroy
@search.destroy
flash[:success] = "Micropost deleted"
redirect_to request.referrer || @searches
end
private
def search_params
#:userid = @user.id
params.require(:search).permit(:searchname, :city, :min_gpa, :max_gpa, :firstname, :state, :city, :age, :gender, :universityname, :language, :livingin, :workexperience, :monthsspentabroadLiving, :monthsspentabroadworking, :degree , :degreetype, :countryofdegree, :wantstoworkin, :hasworkexperiencein, :permissiontoworkin, :currentlyemployed, :referencesuponrequest, :worktype, :charitywork)
end
end
新搜索表单 - 查看:
<%= bootstrap_form_for @search, url: user_searches_path(current_user), html: {class: "pure-form"} do |s| %>
<%= s.hidden_field :userid, :value => current_user.id %>
<div class="field">
<%= s.text_field :searchname, label: "Search Name" %>
</div>
答案 0 :(得分:2)
如果你做了user_search GET /users/:user_id/searches/:id(.:format) searches#show
,你会看到你有这条路线:
searches
这是因为您的users
资源嵌套在current_user
资源下。因此,您需要将@search
和user_search_path
传递给redirect_to user_search_path(current_user, @search)
辅助方法。
因此,重定向到用户搜索页面的正确方法是:
"gallery images"