Hi I’m trying to use a form as a partial for both the new/edit views of my albums controller/model. However, it’s giving me the error when I try to edit the album:
No route matches [PUT] "/users/22/albums"
I think this might have to do with my form’s :url. My form works fine when I submit it to create an album, but gets that error when I try to edit it.
I tried taking out the url: user_albums_path in my form, but it would just give me an error when I try to create a new album.
No route matches [POST] "/albums"
Is there any way to make it so that the form works for both actions? I feel like the :url can’t coexist properly in both actions.
_form.html.erb
<%= form_for (@album), url: user_albums_path, :html => { :id => "uploadform", :multipart => true } do |f| %>
<div class="formholder">
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :description %>
<%= f.text_area :description %>
<br>
<%=f.submit %>
</div>
<% end %>
albums controller
class AlbumsController < ApplicationController
def index
@user = User.find(params[:user_id])
@albums = @user.albums.all
respond_to do |format|
format.html
format.json { render json: @albums }
end
end
def show
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id])
end
def update
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id])
respond_to do |format|
if @album.update_attributes(params[:album])
format.html { redirect_to user_album_path(@user, @album), notice: 'Album successfully updated' }
else
format.html { render 'edit' }
end
end
end
def edit
@user = User.find(params[:user_id])
@album = @user.albums.find(params[:id])
end
def create
@user = User.find(params[:user_id])
@album = @user.albums.build(params[:album])
respond_to do |format|
if @user.save
format.html { redirect_to user_album_path(@user, @album), notice: 'Album was successfully created.' }
format.json { render json: @album, status: :created, location: @album}
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def new
@user = User.find(params[:user_id])
@album = Album.new
end
def destroy
end
end
please help!
update:
FIXED IT! on my own! the form just needed to be <%= form_for([@user, @album])...
Try
That syntax properly scopes the resource routes
Remember to have an
@albuminstance variable if you need one. You can build an empty album using@user.album.build