[英]Controller works fine locally but not on heroku
I'm using carrierwave with amazon s3 and heroku to upload and store images. 我正在将亚马逊S3和heroku与载波结合使用,以上传和存储图像。 It works fine locally but for some reason the controller function "new" isn't being executed on heroku.
它在本地运行良好,但由于某种原因,未在heroku上执行控制器功能“ new”。 I've printed the params[:controller] & params[:action] to the view, and both of them are correct, so I'm not sure what the problem is.
我已经将params [:controller]和params [:action]打印到视图中,并且它们都是正确的,所以我不确定问题出在哪里。
Routes.rb 的routes.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'
images_controller.rb images_controller.rb
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
new.html.erb new.html.erb
<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 %>
heroku error logs heroku错误日志
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]:
Console log 控制台日志
irb(main):001:0> Property.find(1)
Property Load (2.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = $1 LIMIT 1 [["id", 1]]
Property Load (2.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = $1 LIMIT 1 [["id", 1]]
=> #<Property id: 1, listing_status: "In Draft", my_property_reference: "My reference", floor_area: 350, land_area: 500, state: "New Development", city: "Sydney", area: "CBD", street: "123 fake st", price: 800000, floors: 2, property_type: "Apartment", property_age: nil, completion: 2016, bedrooms: 5, bathrooms: 2, under_cover_car_spaces: 3, car_spaces: nil, car_lock_up_garage: 1, additional_information: "Great location in a great area.", description: "great house great house great house great house gr...", created_at: "2016-02-04 03:44:54", updated_at: "2016-02-04 03:46:55", suburb: nil>
irb(main):002:0> Image.all
Image Load (1.8ms) SELECT "images".* FROM "images"
Image Load (1.8ms) SELECT "images".* FROM "images"
=> #<ActiveRecord::Relation []>
irb(main):003:0> Image
=> Image(id: integer, main: boolean, attachment: string, created_at: datetime, updated_at: datetime, property_id: integer)
irb(main):004:0>
The issue is that since you've nested images
under properties
, you'll have to declare both @instance
variables if you want to get the form_for
to work: 问题是,由于您已将
images
嵌套在properties
下,因此,如果要使form_for
起作用,则必须声明两个@instance
变量:
def new
@property = Property.find params[:property_id]
@image = Image.new
end
Of course, you do this already. 当然,您已经这样做了。
The problem, then, is that your Property
with an id
of 1
doesn't exist on your Production environment. 这个问题,那么,你的
Property
有id
的1
不会对您的生产环境中。 You need to make sure you're handling your data-set with the same care as you would development (evaluating if dependent objects don't exist): 您需要确保以与开发相同的方式来处理数据集(评估相关对象是否不存在):
def new
@property = Property.find params[:property_id]
if @property
@image = @property.images.new
else
redirect_to root_path, notice: "No Property"
end
end
You'll also want to define multiple resources
at once in your routes
(to clean it up): 您还需要在
routes
中一次定义多个resources
(以进行清理):
#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'
Update 更新
The solution was trivial, the OP left a photos_controller.rb
file with class ImagesController < ApplicationController
declaration. 解决方案很简单,OP留下了一个
class ImagesController < ApplicationController
声明的photos_controller.rb
文件。 Removing this file fixed the issue. 删除此文件可解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.