1

simple question that I am not able to solve some how.

I am trying to mimic the first few steps of this Railscast Episode. I have a picture-Model and I am trying to instantiate an object of this kind on the index page. Therefor I am using those lines:

index.erb.html

<%= form_for Picture.new do |f| %>
    <%= f.label :image, "Upload" %>
    <%= f.file_field :image, multiple: true %>
<% end %>

But I am getting this error:

undefined method `pictures_path' for #<#<Class:0xb465af0>:0x58fc488>

If I remove the form it works perfectly. Seems simple but I can't solve it. I would appreciate some help.

PicturesController

class PicturesController < ApplicationController
  respond_to :html

  def index
    @house = House.find(params[:house_id])
    @pictures = @house.pictures
    respond_with(@pictures)
  end

  def new
    @picture = Picture.new
  end

  def create
  end

  def destroy
  end

  private

  def picture_params
    params.require(:picture).permit(:id, :name, :house_id, :image, :_destroy)
  end

routes.rb

Rails.application.routes.draw do

  resources :houses do
      resources :pictures, only: [:index]
  end
end
Syk
  • 393
  • 4
  • 19

2 Answers2

1

With your given routes info, you don't really have pictures_path. You have only these routes (if you do a rake routes):

house_pictures GET    /houses/:house_id/pictures(.:format) pictures#index
        houses GET    /houses(.:format)                    houses#index

That's why you are getting that error.

You have access to house_pictures_path BUT NOT pictures_path.

To solve this issue, you have to use house_pictures_path and send the @house and @pictures as argument to that. something like this:

<%= form_for [@house, @house.pictures.build] do |f| %>
    <%= f.label :image, "Upload" %>
    <%= f.file_field :image, multiple: true %>
<% end %>
K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • 1
    Yes, that's it of course :D Thanks for opening my eyes again. Although it has to be `@house.pictures.build` since its one-to-many association. If you change that in your answer I am happy to accept it as solving answer. – Syk Oct 10 '15 at 16:18
  • I have updated my answer. But, did you try `@house.pictures.new` actually? that should work too as it has a `has_many` association with the picture, so `@house.pictures.new` and `@house.pictures.build` should have the same effect. See this for reference: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation.rb#L124 – K M Rakibul Islam Oct 10 '15 at 16:23
  • Yea, you're right. You wrote `@house.picture.new` in your first answer but I should have seen that the missing "s" was the problem and not the method. So both would be correct aslong as its plural. Thanks for your help. – Syk Oct 10 '15 at 16:32
  • No problem. Glad it helped :) – K M Rakibul Islam Oct 10 '15 at 16:33
0

Your pictures resource is nested within your houses resource. There is no route to allow you to create a new Picture without a House to provide the surrounding context, and so form_for cannot automatically produce a URL if you give it only Picture.new.

You need to give it both a house, and a picture.

Typically, your would do something like this:

form_for [@house, @house.pictures.new] do |f|
user229044
  • 232,980
  • 40
  • 330
  • 338