Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 嵌套资源的Rails路由_Ruby On Rails_Ruby_Ruby On Rails 3_Ruby On Rails 4_Routing - Fatal编程技术网

Ruby on rails 嵌套资源的Rails路由

Ruby on rails 嵌套资源的Rails路由,ruby-on-rails,ruby,ruby-on-rails-3,ruby-on-rails-4,routing,Ruby On Rails,Ruby,Ruby On Rails 3,Ruby On Rails 4,Routing,在使用嵌套资源路由时,当尝试查看搜索结果时,我不断收到以下错误消息,如下所示。由于某些原因,我无法让rails使用以下路径和控制器中的重定向到@search调用显示搜索结果 路由。rb: resources :users do resources :searches end NoMethodError in SearchesController#create undefined method `search_url' for #<SearchesController:0x007

在使用嵌套资源路由时,当尝试查看搜索结果时,我不断收到以下错误消息,如下所示。由于某些原因,我无法让rails使用以下路径和控制器中的
重定向到@search
调用显示搜索结果

路由。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
以下是错误信息

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
搜索控制器中的NoMethodError#创建 未定义的方法“搜索url”# 提取的源(第19行附近): 17@search=当前用户.search.create(搜索参数) 18 19将_重定向到@search 20 21完 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>
class SearchesController
新搜索表单-查看:

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>

当前用户id%>

如果您执行一个
bundle exec rake routes
,您将看到您有这个路由:

user_search GET    /users/:user_id/searches/:id(.:format)      searches#show
这是因为您的
搜索
资源嵌套在
用户
资源下。因此,您需要将
当前用户
@search
传递给
用户搜索路径
助手方法

因此,重定向到用户搜索页面的正确方法是:

redirect_to user_search_path(current_user, @search)

很高兴我能帮忙:-)