1

As a follow up to this earlier question, which has been solved

I have made a beginning with a rails app where I can create Collections. Each Collections is able to have multiple Photos. I can now create these Collections and Photos. But whenever I try to visit /collections/1/photos it has a problem with this line in my photos index

undefined method `photo_path' for #<#<Class:0x007fb8c40f1b38>:0x007fb8c88673a0>

<td><%= link_to 'Show', photo %></td>

photos_controller.rb

class PhotosController < ApplicationController
  before_action :set_photo, only: [:show, :edit, :update, :destroy]

  # GET /photos
  # GET /photos.json
  def index
    @photos = Photo.all
  end

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

  # GET /photos/new
  def new
    @photo = Photo.new
  end

  # GET /photos/1/edit
  def edit
  end

  # POST /photos
  # POST /photos.json
  def create
    @photo = Photo.new(photo_params)

    respond_to do |format|
      if @photo.save
        redirect_to @photo
        format.html { redirect_to @photo, notice: 'Photo was successfully created.' }
        format.json { render :show, status: :created, location: @photo }
      else
        format.html { render :new }
        format.json { render json: @photo.errors, status: :unprocessable_entity }
      end
    end
  end

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

  # DELETE /photos/1
  # DELETE /photos/1.json
  def destroy
    @photo.destroy
    respond_to do |format|
      format.html { redirect_to photos_url, notice: 'Photo was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

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

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

routes.rb

Rails.application.routes.draw do
  resources :collections do
    resources :photos
  end
end

/photos/index.html

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

<h1>Photos</h1>

<table>
  <thead>
    <tr>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @photos.each do |photo| %>
      <tr>
        <td><%= photo.name %></td>
        <td><%= link_to 'Show', photo %></td>
        <td><%= link_to 'Edit', edit_photo_path(photo) %></td>
        <td><%= link_to 'Destroy', photo, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'New Photo', new_collection_photo_path %>

collection.rb

class Collection < ApplicationRecord
  has_many :photos
end

photo.rb

class Photo < ApplicationRecord
  belongs_to :collection
end

I use Rails 5.0.0.1 and ruby 2.3.0

1
  • Run rake routes. Seems like correct path will be: collection_photos_path Commented Nov 25, 2016 at 16:36

1 Answer 1

1

Use this your show and delete link path is wrong

<td><%= link_to 'Show', collection_photo_path(@collection, photo) %></td>
<td><%= link_to 'Edit', edit_collection_photo_path(@collection, photo) %></td>
<td><%= link_to 'Destroy', 
collection_photo_path(@collection, photo), method: :delete, data: { confirm: 'Are you sure?' } %></td>

Links should be made this , from controller you should intialize @collection

def index
  @collction = Collection.find(params[:collection_id])
  @photos = @collction.photos
end
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot!! I already had a feeling I was missing something in my controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.