控制器在本地工作正常,但在heroku上没有

时间:2016-02-06 07:19:26

标签: ruby-on-rails heroku amazon-s3 carrierwave

我使用了带有amazon s3和heroku的carrierwave来上传和存储图像。它在本地工作正常但由于某种原因控制器功能"新"没有在heroku上执行。 我打印了params [:controller]& params [:action]到视图,并且它们都是正确的,所以我不确定问题是什么。

的routes.rb

     <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="windowActionBarOverlay">false</item>

        <item name="windowActionBar">false</item>

        <item name="windowNoTitle">true</item>
        <item name="android:windowBackground">@color/background_material_light</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>

images_controller.rb

      Rails.application.routes.draw do
  resources :surroundings

  resources :headings

  resources :options

  resources :categories

  resources :mains

  resources :properties do


    resources :images, only: [:index, :new, :create, :destroy] do
    collection do 
    get 'update_main' 
    end

    end


    get 'addoption'
    post 'postoption'

end

root 'search#main'
get 'main' =>'search#main'

new.html.erb

class ImagesController < ApplicationController




def new
#redirect_to property_images_path #used for trouble shooting - does not redirect
@property=Property.find(params[:property_id])
@image = Image.new

end


 def index
    @images = Property.find(params[:property_id]).images
  end



def update_main
#set the current main image to false
   r=Property.find(params[:property_id]).images.where("main" => true).first
   r.main=false unless r.nil?
   r.save unless r.nil?
   @image=Image.find(params[:main])
   @image.main=true;
   @image.save
 redirect_to property_images_path, notice: "The Image has been uploaded."
end


  def create
    @image = Image.new(image_params)
    @image.property_id=params[:property_id]
    if @image.save
      redirect_to property_images_path, notice: "The Image has been uploaded."
    else
      render "new"
    end
  end

  def destroy
    @image = Image.find(params[:id])
    @image.destroy
    redirect_to property_images_path, notice:  "The Image  has been deleted."
  end

private
  def image_params
   params.require(:image).permit(:attachment,:property_id)
  end
end

heroku错误日志

<h1>Add new image</h1>
<%=params[:controller]%>
<%=params[:action]%>


<%= form_for([@property,@image]) do |f| %>
 <%= label_tag("Select image file") %>
   <%= f.file_field :attachment, class:"btn btn-default" %> 

<br>

    <%= f.submit class:"btn btn-primary" %>

<% end %>

控制台日志

Processing by ImagesController#new as HTML
2016-02-06T07:43:25.314010+00:00 app[web.1]:   Parameters: {"property_id"=>"1"}
2016-02-06T07:43:25.519196+00:00 heroku[router]: at=info method=GET path="/properties/1/images/new" host=auspropguides.herokuapp.com request_id=ecaefc40-5556-4239-9da6-1a154fa489f3 fwd="14.201.25.50" dyno=web.1 connect=0ms service=391ms status=500 bytes=1754
2016-02-06T07:43:25.509640+00:00 app[web.1]:   Rendered images/new.html.erb within layouts/application (1.9ms)
2016-02-06T07:43:25.512962+00:00 app[web.1]: 
2016-02-06T07:43:25.512964+00:00 app[web.1]: ActionView::Template::Error (First argument in form cannot contain nil or be empty):
2016-02-06T07:43:25.509818+00:00 app[web.1]: Completed 500 Internal Server Error in 196ms
2016-02-06T07:43:25.512968+00:00 app[web.1]:     7:  <%= label_tag("Select image file") %>
2016-02-06T07:43:25.512969+00:00 app[web.1]:     8:    <%= f.file_field :attachment, class:"btn btn-default" %> 
2016-02-06T07:43:25.512965+00:00 app[web.1]:     3: <%=params[:action]%>
2016-02-06T07:43:25.512967+00:00 app[web.1]:     5: 
2016-02-06T07:43:25.512968+00:00 app[web.1]:     6: <%= form_for([@property,@image]) do |f| %>
2016-02-06T07:43:25.512966+00:00 app[web.1]:     4: 
2016-02-06T07:43:25.512971+00:00 app[web.1]:   app/views/images/new.html.erb:6:in `_app_views_images_new_html_erb___892626609660663212_69968138740340'
2016-02-06T07:43:25.512972+00:00 app[web.1]: 
2016-02-06T07:43:25.512970+00:00 app[web.1]:     9: 
2016-02-06T07:43:25.512972+00:00 app[web.1]: 

1 个答案:

答案 0 :(得分:0)

问题在于,由于您已在images下嵌套properties,因此如果要让@instance工作,则必须声明两个form_for变量:

def new
  @property = Property.find params[:property_id]
  @image    = Image.new
end

当然,你已经这样做了。

问题是,您的生产环境中不存在Property id 1的{​​{1}}。您需要确保以与开发相同的方式处理数据集(评估依赖对象是否不存在):

def new
   @property = Property.find params[:property_id]
   if @property 
      @image = @property.images.new
   else
     redirect_to root_path, notice: "No Property" 
   end
end

您还需要在routes中定义multiple resources at once(以清理它):

#config/routes.rb
resources :surroundings, :headings, :options, :categories, :mains
resources :properties do
  resources :images, only: [:index, :new, :create, :destroy] do
    get 'update_main', on: :collection
  end
  get  :addoption
  post :postoption
end

resources :search, path: "", only: [] do
  get :main, on: :collection
end

root 'search#main'

<强>更新

解决方案很简单,OP留下photos_controller.rb文件,声明为class ImagesController < ApplicationController。删除此文件解决了问题。