stackov8
stackov8

Reputation: 434

how to make a form for nested paths?

please help solve the problem. i have nested routes:

  resources :users do
    resources :albums
  end

models:

class User < ActiveRecord::Base
  devise  :database_authenticatable, 
          :registerable,
          :recoverable, 
          :rememberable, 
          :trackable, 
          :validatable

  has_many :albums  
end

class Album < ActiveRecord::Base
  belongs_to :user
end

album controller:

  def new
    @album = Album.new
  end

  def create
    @album = Album.new(album_params)

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

  def set_album
    @album = Album.find(params[:id])
  end  

views/albums/new.html.erb:

<%= form_for [@user, @album] do |f| %>
  <%= f.text_area :album %>
  <%= f.submit %>
<% end %>

routes:

........
..........
             user_albums GET    /users/:user_id/albums(.:format)          albums#index
                         POST   /users/:user_id/albums(.:format)          albums#create
          new_user_album GET    /users/:user_id/albums/new(.:format)      albums#new
         edit_user_album GET    /users/:user_id/albums/:id/edit(.:format) albums#edit
              user_album GET    /users/:user_id/albums/:id(.:format)      albums#show
                         PATCH  /users/:user_id/albums/:id(.:format)      albums#update
                         PUT    /users/:user_id/albums/:id(.:format)      albums#update
                         DELETE /users/:user_id/albums/:id(.:format)      albums#destroy
                   users GET    /users(.:format)                          users#index
                         POST   /users(.:format)                          users#create
                new_user GET    /users/new(.:format)                      users#new
               edit_user GET    /users/:id/edit(.:format)                 users#edit
                    user GET    /users/:id(.:format)                      users#show
                         PATCH  /users/:id(.:format)                      users#update
                         PUT    /users/:id(.:format)                      users#update
                         DELETE /users/:id(.:format)                      users#destroy
............
..........

in result i get follow errormessage:

NoMethodError in Albums#new Showing /home/kalinin/rails/phs/app/views/albums/_form.html.erb where line #1 raised: undefined method `albums_path' for

<#:0x007fdbe06745a8>

please help make new-form

Upvotes: 0

Views: 23

Answers (1)

Pavan
Pavan

Reputation: 33542

undefined method `albums_path'

The error is because you didn't have @user initialized in your controller new method, so @user is nil. So Rails consider it as <%= form_for @album %> which triggers that error as there is no albums_path.

Try

<%= form_for [current_user, @album] do |f| %>

Upvotes: 1

Related Questions