Routing error when trying to use same view for update and create flows (Rails 3)

Posted by Jamis Charles on Stack Overflow See other posts from Stack Overflow or by Jamis Charles
Published on 2011-01-13T13:32:22Z Indexed on 2011/01/14 4:53 UTC
Read the original article Hit count: 197

Filed under:
|

My overall use case:

I have a Listing model that has many images. The Listing detail page lists all the fields that can be updated inline (through ajax).

I want to be able to use the same view for both update listing and create new listing.

My listing controller looks as follows:

 def detail 
     @listing = Listing.find(params[:id])  
     @image = Image.new #should this link somewhere else?

     respond_to do |format| 
         format.html # show.html.erb  
         format.xml { render :xml => @listing } 
      end 
 end 


  def create
      # create a new listing and save it immediately. Assign it to guest, with a status of "draft"
      @listing = Listing.new(:price_id => 1)  # Default price id
      # save it to db
      # TODO add validation that it has to have a price ID, on record creation. So the view doesn't break.
      @listing.save
      @image = Image.new

      # redirect_to "/listings/detail/@listing.id" #this didn't work

      respond_to do |format| 
          format.html # show.html.erb  
          format.xml { render :xml => @listing } 
      end
 end 


The PROBLEM
I'm using a partial that shows the same form for the create view and the detail view.

This works perfectly except for one thing: When I pull up
http://0.0.0.0:3000/listings/detail/7, it works perfectly.

When I pull up
http://0.0.0.0:3000/listings/new, I get the following error:

Showing /Applications/MAMP/htdocs/rails_testing/feedbackd/app/views/listings/_edit_form.html.erb where line #100 raised:

No route matches {:action=>"show", :controller=>"images"} Extracted source (around line #100):

97: <!-- Form for new images -->
98: <div class="span-20 append-bottom">
99: <!-- <%# form_for :image, @image, :url => image_path, :html => { :multipart => true } do |f| %> -->
100: <%= form_for @image, :url => image_path, :html => { :multipart => true } do |f| %>
101:     <%= f.text_field :description %><br />
102:  <%= f.file_field :photo %>
103:  <%= submit_tag "Upload" %>

What I think the issue is: When I upload a new image (I'm using Paperclip), it requires the listing_id to create the image record. Since the listing_id isn't passed in with listings/new it can't find the listing_id. How can I pass in the id? Via a redirect? What's the best way to solve this? Thank you.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about paperclip