0

My error says:

No route matches {:action=>"new", :advertiser_id=>"2", :controller=>"analyses", :experiment_id=>nil} missing required keys: [:experiment_id]

Error happens in

app/views/analyses/index.html.erb:29:in `_app_views_analyses_index_html_erb___267178907_46340268'

Which points to this piece of code in my index.html.erb

<%= link_to 'New Analysis',  new_advertiser_experiment_analysis_path(params[:experiment_id]) %>

The error also shows the parameters being passed as:

Parameters: {"advertiser_id"=>"9", "experiment_id"=>"2"}

Not sure why its telling me it doesnt know the experiment_id.

Here is my Analyses Controller

class AnalysesController < ApplicationController
  before_action :set_analyses
  before_action :set_analysis, only: [:show, :edit, :update, :destroy]


  # GET experiments/1/analyses
  def index
    @analyses = @experiment.analyses
  end

  # GET experiments/1/analyses/1
  def show
  end

  # GET experiments/1/analyses/new
  def new
    @analysis = @experiment.analyses.build
  end

  # GET experiments/1/analyses/1/edit
  def edit
  end

  # POST experiments/1/analyses
  def create
    @analysis = @experiment.analyses.build(analysis_params)

    if @analysis.save
      redirect_to([@analysis.experiment, @analysis], notice: 'Analysis was successfully created.')
    else
      render action: 'new'
    end
  end

  # PUT experiments/1/analyses/1
  def update
    if @analysis.update_attributes(analysis_params)
      redirect_to([@analysis.experiment, @analysis], notice: 'Analysis was successfully updated.')
    else
      render action: 'edit'
    end
  end

  # DELETE experiments/1/analyses/1
  def destroy
    @analysis.destroy

    redirect_to experiment_analyses_url(@experiment)
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_analyses
      @experiment = Experiment.find(params[:experiment_id])
    end

    def set_analysis
      @analysis = @experiment.analyses.find(params[:id])
    end

    # Only allow a trusted parameter "white list" through.
    def analysis_params
      params.require(:analysis).permit(:conversion, :aov, :win_loss, :notes)
    end

end
Leo LeGendre
  • 63
  • 2
  • 10

1 Answers1

0

From your current code it shows that you have three level nesting for resources. e-g:

resources :advertiser do 
  resources :experiment do 
  resources :analysis
  end 
end

so route new_advertiser_experiment_analysis_path expects two ids. one for advertiser and other for experiment so you are just passing single id which is consider as advertiser_id (It follows the order in which we provide id's if not mentioned explicitily by hash) so it get nil for experiment_id that's why it fails. so you need

<%= link_to 'New Analysis',  new_advertiser_experiment_analysis_path(advertiser_id: params[:advertiser_id] , experiment_id: params[:experiment_id]) %>

Check Rails Nested Routing for complete guide.

Qaisar Nadeem
  • 2,404
  • 13
  • 23