momchenr
momchenr

Reputation: 275

before_filter for nested resource

I have nested resources - users have_many manufacturers and manufacturers have_many lines. I wrote a before_filter to load the @manufacturer so it could go through the rest of the functions in the lines_controller. Problem is, I'm running into issues when I click edit on the lines/show view.

Error: Couldn't find Manufacturer with id=manufacturer_id

77   def load_manufacturer
78     @manufacturer = Manufacturer.find(params[:manufacturer_id])
79   end

So here's what I'm working with:

app/views/lines/show.html.erb

<p id="notice"><%= notice %></p>

<p>
  <strong>Name:</strong>
  <%= @line.name %>
</p>

<p>
  <strong>Manufacturer:</strong>
  <%= @line.manufacturer_id %>
</p>

<%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,@line) %> |
<%= link_to 'Back', manufacturer_lines_path(@line.manufacturer_id) %>

lines_controller.rb

class LinesController < ApplicationController
  before_action :set_line, only: [:show, :edit, :update, :destroy]
  before_filter :load_manufacturer

  # GET /lines
  # GET /lines.json
  def index
    @lines = Line.all
  end

  # GET /lines/1
  # GET /lines/1.json
  def show
  end

  # GET /lines/new
  def new
    @manufacturer = Manufacturer.find(params[:manufacturer_id])
    @line = @manufacturer.lines.build
  end

  # GET /lines/1/edit
  def edit
  end

  # POST /lines
  # POST /lines.json
  def create
    @line = @manufacturer.lines.build(line_params)

    respond_to do |format|
      if @line.save
        format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully created.' }
        format.json { render action: 'show', status: :created, location: @line }
      else
        format.html { render action: 'new' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /lines/1
  # PATCH/PUT /lines/1.json
  def update
    respond_to do |format|
      if @line.update(line_params)
        format.html { redirect_to manufacturer_line_path(@manufacturer, @line), notice: 'Line was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @line.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /lines/1
  # DELETE /lines/1.json
  def destroy
    @line.destroy
    respond_to do |format|
      format.html { redirect_to manufacturer_lines_url(@manufacturer) }
      format.json { head :no_content }
    end
  end

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

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_params
      params.require(:line).permit(:name, :manufacturer_id)
    end

    def load_manufacturer
      @manufacturer = Manufacturer.find(params[:manufacturer_id])
    end
end

Upvotes: 0

Views: 401

Answers (1)

engineersmnky
engineersmnky

Reputation: 29488

This:

 <%= link_to 'Edit', edit_manufacturer_line_path(:manufacturer_id,@line) %>

Should be

<%= link_to 'Edit', edit_manufacturer_line_path(@manufacturer,@line) %> 

What is happening is it is changing the symbol :manufacturer_id to a string and passing it to the route so your route looks like

`/manufacturers/manufacturer_id/lines/###/edit`

when you want

/manufacturers/###/lines/###/edit

Upvotes: 3

Related Questions