[英]Controller works fine locally but not on heroku
我正在將亞馬遜S3和heroku與載波結合使用,以上傳和存儲圖像。 它在本地運行良好,但由於某種原因,未在heroku上執行控制器功能“ new”。 我已經將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" = $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>
問題是,由於您已將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
中一次定義多個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'
更新
解決方案很簡單,OP留下了一個class ImagesController < ApplicationController
聲明的photos_controller.rb
文件。 刪除此文件可解決此問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.