2

I am using carrierwave and remotipart to handle an AJAX form where I upload an album of music and accompanying information to a website. After submitting the form, I would like to redirect the partial where the form was located to the newly created album's page. The form, located in \albums\_new.html.erb looks like this:

<h1>Add a new album</h1>
<div>
    <%= form_for(album, remote: true, url: {action: "create"}, html: {multipart: true}) do |f|%>
        <h2>Information: </h2>
        <div>
            <%= f.label :title %>
            <%= f.text_field :title %>
            <%= f.label :year %>
            <%= f.text_field :year %>
            <%= f.label :image %>
            <%= f.file_field :image %>
        </div>

        ...

        <%= f.submit "Add Album" %>
    <% end %>
</div>

The controller's new and create methods look like this:

def new
  @album = Album.new
  @album.producers.build
  @album.tracks.build
end

def create
  respond_to do |format|
    @album = Album.new(album_params)
    if @album.save
      format.js {redirect_to album_path(@album)}
    end
  end
end

The redirect currently only works when I submit the form without any file attachments. If I do try to include a file attachment, the upload still succeeds but the redirect fails and I get the following message in my console:

ActionView::MissingTemplate (Missing template albums/show,
application/show with {:locale=>[:en], :formats=>[:html], :variants=>[],
:handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}.

I have been able to implement a workaround where I simply call the same expression in create.js.erb that I do in show.js.erb:

$('#listen-window').html("<%= j (render partial: 'show', locals: {album: @album, tracks: @tracks, producers: @producers}) %>")

and write a create method as such:

def create
  respond_to do |format|
    @album = Album.new(album_params)
    if @album.save
      @tracks = @album.tracks
      @producers = @album.producers
      format.js
    end
  end
end

Functionally, this code does achieve what I want. However, it does not make sense to me from a routing perspective. How would you write a more elegant solution?

Bill Jia
  • 21
  • 2
  • why do you avoid the `format.html ` response? – Demi Magus Jun 16 '15 at 01:26
  • I only want the result to render in part of the browser. I'm rather new to Rails development, but from what I understand, you need the `format.js` response to load using AJAX? – Bill Jia Jun 16 '15 at 01:34
  • yes, `format.js` will be used for ajax response, but you also can have an html response with the json response `format.html` unless you don't want to load the html view – Demi Magus Jun 16 '15 at 01:37
  • I have other things going on client-side in other partials while the user might be uploading files, so unless there's another way to not disturb them, I don't think I want to load the HTML view. – Bill Jia Jun 16 '15 at 01:47
  • the have you tried `format.json { head :no_content }` this will not render the html view and it will give a 204 response to the ajax request, which is an empty response – Demi Magus Jun 16 '15 at 01:58

0 Answers0