将paperclip用于嵌套资源时的NoMethodError

时间:2013-05-21 11:11:50

标签: ruby-on-rails paperclip

您好我是Rails的新手,我设法让一些课程一起工作,直到我尝试使用Paperclip。

在我尝试上传图片之前,一切正常。我收到这个错误:
#<#:0x4eecc88>

的未定义方法`business_locations_path'

以下是两种型号:

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

要创建新位置,路径将类似于:

http://localhost:3000/businesses/8/businessLocations/new

表格:

<%
 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 } do |f|   %> 
...

<div class="field">
<%= f.label :mainpic %><br />
<%= f.file_field :mainpic %>
 </div>

BusinessLocation控制器:

def create
    respond_to do |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 was successfully 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

没有这样的路径business_locations_path,因为BusinessLocation嵌套在Business中。

编辑:这是我的佣金路线:

    business_businessLocations GET        /businesses/:business_id/businessLocations(.:format) businessLocations#index    
                               POST       /businesses/:business_id/businessLocations(.:format) businessLocations#create 
    new_business_businessLocation GET   /businesses/:business_id/businessLocations/new(.:format)       businessLocations#new
    edit_business_businessLocation GET   /businesses/:business_id/businessLocations/:id/edit(.:format)  businessLocations#edit
 business_businessLocation GET     /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 GET        /businesses(.:format)
                     businesses#index
                           POST       /businesses(.:format)
                     businesses#create
              new_business GET        /businesses/new(.:format)
                     businesses#new
             edit_business GET        /businesses/:id/edit(.:format)
                     businesses#edit
                  business GET        /businesses/:id(.:format)
                     businesses#show
                           PUT        /businesses/:id(.:format)
                     businesses#update
                           DELETE     /businesses/:id(.:format)
                     businesses#destroy

1 个答案:

答案 0 :(得分:0)

在您的控制器中,您将创建操作设置为重定向到business_businessLocation_path。这是由您用于生成嵌套脚手架的任何内容自动生成的。修复此问题可能会解决您的所有问题。