ruby on rails - NoMethodError when using paperclip for nested resource -
hi pretty new rails, , managed few classes working until tried use paperclip.
everything works fine until try upload picture. error:
undefined method `business_locations_path' #<#:0x4eecc88>
these 2 models:
class business < activerecord::base attr_accessible :bizname, :contact, :email, :ownername, :signupdate, :website has_many :businesslocations end class businesslocation < activerecord::base belongs_to :business attr_accessible :address1, :address2, :contact, :description, :email, :latitude, :longitude, :postal, :price, :mainpic has_attached_file :mainpic, :styles => {:large => "640x640>", :medium => "320x320>", :thumb => "100x100>"} end
to create new location, path like:
http://localhost:3000/businesses/8/businesslocations/new
the form:
<% if params[:action] == "new" urlpath = business_businesslocations_path(@business) else if params[:action] == "edit" urlpath = business_businesslocation_path(@business,@businesslocation) end end %> <%= form_for @businesslocation, :url=>urlpath, :html =>{ :multipart => true } |f| %> ... <div class="field"> <%= f.label :mainpic %><br /> <%= f.file_field :mainpic %> </div>
the businesslocation controller:
def create respond_to |format| @business = business.find(params[:business_id]) @businesslocation = @business.businesslocations.build(params[:business_location]) if @businesslocation.save format.html { redirect_to(business_businesslocation_path(@business,@businesslocation), :notice => 'post created.') } format.json { render :json => @businesslocation, :status => :created, :location => @business } else format.html { render :action => "new" } format.json { render :json => @businesslocation.errors, :status => :unprocessable_entity } end end end
there no such path business_locations_path because businesslocation nested in business.
edit: here rake routes:
business_businesslocations /businesses/:business_id/businesslocations(.:format) businesslocations#index post /businesses/:business_id/businesslocations(.:format) businesslocations#create new_business_businesslocation /businesses/:business_id/businesslocations/new(.:format) businesslocations#new edit_business_businesslocation /businesses/:business_id/businesslocations/:id/edit(.:format) businesslocations#edit business_businesslocation /businesses/:business_id/businesslocations/:id(.:format) businesslocations#show put /businesses/:business_id/businesslocations/:id(.:format) businesslocations#update delete /businesses/:business_id/businesslocations/:id(.:format) businesslocations#destroy businesses /businesses(.:format) businesses#index post /businesses(.:format) businesses#create new_business /businesses/new(.:format) businesses#new edit_business /businesses/:id/edit(.:format) businesses#edit business /businesses/:id(.:format) businesses#show put /businesses/:id(.:format) businesses#update delete /businesses/:id(.:format) businesses#destroy
in controller, have create action set redirect business_businesslocation_path
. autogenerated whatever used genereate nested scaffold. fixing fix problems.
Comments
Post a Comment