2015-04-08 4 views
0

Я следил за этим Railscasts tutorial, чтобы разрешить импорт данных Excel. Мое приложение может импортировать данные, однако я хочу перенаправить на один экземпляр, а не на индексную страницу, и это сложно.Перенаправление, отображаемое после импорта Excel

Когда у меня установлен мой контроллер на redirect_to quotes_path, я перенаправлен на индекс котировок без проблем. Однако, когда я меняю его на redirect_to @quote или redirect_to quote_path(@quote) от redirect_to quotes_path(@quote), но ни одна из них не работает. Когда процессы переадресацией, я получаю:

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

Вот мой 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 

Пожалуйста, дайте мне знать, если я могу предоставить любую дополнительную информацию.

Благодаря загодя!

+0

Кто-нибудь еще? У меня все еще есть проблемы. Спасибо! –

ответ

0

Ваш before_action, #set_quote не настроен для запуска до действия #import, поэтому @quote равен нулю при использовании помощника quote_path. Затем помощник не знает идентификатор цитаты, для которой вы хотите показать путь. Если вы добавите: import в массив методов в before_action, он должен работать.

+0

Привет, Джек. Спасибо за ответ. Ваше решение помогло мне перейти на новую ошибку, так что частичная победа, на мой взгляд. Теперь я получаю эту ошибку: 'Не удалось найти Quote с 'id' =' 'ActiveRecord :: RecordNotFound (не удалось найти предложение с 'id' =): app/controllers/quotes_controller.rb: 84: in 'set_quote'' –

Смежные вопросы