0

嗨,我对 Rails 很陌生,在我尝试使用 Paperclip 之前,我设法让一些类一起工作。

一切正常,直到我尝试上传图片。我收到此错误:
#<#:0x4eecc88> 的未定义方法 `business_locations_path'

这些是2个模型:

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
4

1 回答 1

0

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

于 2013-05-21T14:23:23.787 回答