0

I followed thisRailscasts tutorial in order to allow importing of Excel data. My app is able to import data, however, I want to redirect to one instance rather than the index page, and this is proving to be difficult.

When I have my controller set to redirect_to quotes_path, I am redirected to the quotes index without issue. However, when I change the it to redirect_to @quote or redirect_to quote_path(@quote) from redirect_to quotes_path(@quote) but neither option works. When the redirect processes, I get:

    ActionController::UrlGenerationError (No route matches {:action=>"show", :controller=>"quotes", :id=>nil} missing required keys: [:id]):
  app/controllers/quotes_controller.rb:10:in `import'

Here is my quotes_controller:

    class QuotesController < ApplicationController

  before_action :set_quote, only: [:show, :edit, :update, :destroy]

  def import

    Employee.import(params[:file])
    # redirect_to @quote,  notice: "Census imported."
    redirect_to quote_path(@quote)
  end

  # GET /quotes
  # GET /quotes.json
  def index
    @quotes = Quote.all
    @clients = Client.all
    @employees = Employee.all
  end

  # GET /quotes/1
  # GET /quotes/1.json
  def show
    @quote = Quote.find params[:id]
    @quotes = Quote.all
    @employees = Employee.all
    @employee = Employee.find_by(company_name: @quote.company_name)
    @client = Client.find_by(company_name: @quote.company_name)
    @clients = Client.all
  end

  # GET /quotes/new
  def new
    @quote = Quote.new
    @employee = Employee.new
  end

  # GET /quotes/1/edit
  def edit
  end

  # POST /quotes
  # POST /quotes.json
  def create
    @quote = Quote.new(quote_params)
    respond_to do |format|
      if @quote.save
        format.html { redirect_to @quote, notice: 'Quote was successfully created.' }
        format.json { render :show, status: :created, location: @quote }
      else
        format.html { render :new }
        format.json { render json: @quote.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /quotes/1
  # PATCH/PUT /quotes/1.json
  def update
    respond_to do |format|
      if @quote.update(quote_params)
        format.html { redirect_to @quote, notice: 'Quote was successfully updated.' }
        format.json { render :show, status: :ok, location: @quote }
      else
        format.html { render :edit }
        format.json { render json: @quote.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /quotes/1
  # DELETE /quotes/1.json
  def destroy
    @quote.destroy
    respond_to do |format|
      format.html { redirect_to quotes_url, notice: 'Quote was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_quote
      @quote = Quote.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def quote_params
      params.require(:quote).permit(:company_name, :quote_name, :premium_total, :eff_date, :active)
    end

    def employee_params
      params.require(:employee).permit(:company_name, :family_id, :first_name, :last_name, :dob, :sub_status, :gender, :uses_tobacco, :tobacco_cessation, :emp_status, :coverage_type, :currently_enrolled, :current_anthem, :current_plan_id, :quote_id, :premium)
    end
end

Please let me know if I can provide any additional information.

Thanks ahead of time!

1 Answers1

0

Your before_action, #set_quote isn't set up to run before the #import action so @quote is nil when you use the quote_path helper. The helper then doesn't know the id of the quote you want a show path for. If you add :import to the array of methods in the before_action it should work.

Jack Noble
  • 2,018
  • 14
  • 12
  • Hi Jack. Thanks for the response. Your solution helped me get to a new error, so a partial victory in my opinion. I am now getting this error: `Couldn't find Quote with 'id'= ` `ActiveRecord::RecordNotFound (Couldn't find Quote with 'id'=): app/controllers/quotes_controller.rb:84:in `set_quote'` – Michael Miller Apr 09 '15 at 15:37