jeudi 31 décembre 2015

Error showing search results when using nested resource routes - Rails

Long time reader, first time poster! I keep receiving the below error message when trying to view the results of a search when using nested resource routes as below. For some reason I can't get rails to show the results of my search using the following routes and using the redirect_to @search call in the controller.

routes.rb:

resources :users do
    resources :searches
end

Below is the Error:

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

Searches Controller:

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

New Search Form - View:

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

Aucun commentaire:

Enregistrer un commentaire