0

this is the first time working with polymorphic associations. I have a picture model that belongs to a user model and a scoreboard model. The migration for the picture model is given below.

class CreatePictures < ActiveRecord::Migration
  def change
    create_table :pictures do |t|
      t.string :picture
      t.references :imageable, index: true, polymorphic: true

      t.timestamps null: false
    end
    add_foreign_key :pictures, :imageables
  end

  add_index :pictures, :imageable_id
  # add index on the pictues table on the imageable_id column
end

The belongs_to and has_one associations code is given below.

class Scoreboard < ActiveRecord::Base
  has_one :picture, as: :imageable
end

class User < ActiveRecord::Base
  has_one :picture, as: :imageable
end

class Picture < ActiveRecord::Base
  belongs_to :imageable, polymorphic: true
end

The scoreboard_picture resource is nested inside the scoreboards resource.

resources :scoreboards do 
   resources :scoreboard_picture, only: [:create, :destroy, :new]
end

I have scoreboard_picture controller with the following new method.

class ScoreboardPictureController < ApplicationController

    def new
        @scoreboard = Scoreboard.find(params[:scoreboard_id])
        @picture = @scoreboard.build_picture
    end

New Action

 <%= form_for([@scoreboard, @picture], url: scoreboards_picture_path, html: {multipart: true}) do |f| %>
 <%= f.file_field :picture, accept: 'image/jpeg,image/gif,image/png', id: "files" %>
 <% end %>

I have implemented nested routes before and it hasn't given me any problems. However, I keep getting the following error.

 undefined local variable or method `scoreboards_picture_path' for #<#<Class:0x007fa4c054f490>:0x00000007574700>

I think it has something to do with polymorphic associations. I tried providing the URL in the form. It still doesn't work. I read a few other posts on this question and tried using what worked for them, still getting the error. I am pretty sure my new method is correct. I've been stuck on this for a while. Any help would be greatly appreciated.

5
  • It probably needs to be scoreboard_picture_path. You can confirm this by running rake routes and looking at the left column.
    – dwenzel
    Dec 7, 2015 at 3:47
  • it should be scoreboards_scoreboard_picture_path Dec 7, 2015 at 3:51
  • @PardeepDhingra I tried that previously and it didn't work. I tried it now as well. Not sure whats the problem
    – kpaul
    Dec 7, 2015 at 15:08
  • @dwenzel I tried that and it didn't work.
    – kpaul
    Dec 7, 2015 at 15:09
  • Running rake routes helped. It was new_scoreboard_scoreboard_picture_path. Thanks for the help!
    – kpaul
    Dec 7, 2015 at 15:35

0

Your Answer

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

Browse other questions tagged or ask your own question.