I'm using carrierwave with amazon s3 and heroku to upload and store images. It works fine locally but for some reason the controller function "new" isn't being executed on heroku. 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.
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 error logs
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:
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. 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):
#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. Removing this file fixed the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.