控制器在本地工作正常但在 heroku 上不工作
Controller works fine locally but not on heroku
我在 amazon s3 和 heroku 上使用 carrierwave 来上传和存储图像。它在本地运行良好,但由于某种原因,控制器功能 "new" 未在 heroku 上执行。
我已经把 params[:controller] 和 params[:action] 打印到视图上了,它们都是正确的,所以我不确定是什么问题。
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
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
<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 错误日志
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]:
控制台日志
irb(main):001:0> Property.find(1)
Property Load (2.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = LIMIT 1 [["id", 1]]
Property Load (2.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = 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>
问题在于,由于您已将 images
嵌套在 properties
下,如果您想获得 form_for
,则必须声明两个 @instance
变量上班:
def new
@property = Property.find params[:property_id]
@image = Image.new
end
当然,你已经这样做了。
那么,问题是您的 Property
和 id
为 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 留下了一个带有 class ImagesController < ApplicationController
声明的 photos_controller.rb
文件。删除这个文件解决了这个问题。
我在 amazon s3 和 heroku 上使用 carrierwave 来上传和存储图像。它在本地运行良好,但由于某种原因,控制器功能 "new" 未在 heroku 上执行。 我已经把 params[:controller] 和 params[:action] 打印到视图上了,它们都是正确的,所以我不确定是什么问题。
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
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
<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 错误日志
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]:
控制台日志
irb(main):001:0> Property.find(1)
Property Load (2.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = LIMIT 1 [["id", 1]]
Property Load (2.2ms) SELECT "properties".* FROM "properties" WHERE "properties"."id" = 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>
问题在于,由于您已将 images
嵌套在 properties
下,如果您想获得 form_for
,则必须声明两个 @instance
变量上班:
def new
@property = Property.find params[:property_id]
@image = Image.new
end
当然,你已经这样做了。
那么,问题是您的 Property
和 id
为 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 留下了一个带有 class ImagesController < ApplicationController
声明的 photos_controller.rb
文件。删除这个文件解决了这个问题。