Nidhin S G
Nidhin S G

Reputation: 1695

Ruby on Rails Error. Processing Controller method as png

I have given this in my route.

get '/custom_page/:name' => 'custom_page#load_content'

And this is my controller method.

def load_content
    page_name = (params[:name]).split("_").join(" ")
    p "---------------------"
    p page_name
end

Thing is im getting 2 get calls inside my console. and hence an error. here is what my console looks like..

Started GET "/en/custom_page/Nidhin_Test_Page" for 127.0.0.1 at 2014-05-12 11:50:34 +0530
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT `schema_migrations`.* FROM `schema_migrations`
Processing by CustomPageController#load_content as HTML
  Parameters: {"locale"=>"en", "name"=>"Nidhin_Test_Page"}
  "---------------------"    
  "Nidhin Test Page"
  Rendered custom_page/load_content.html.erb within layouts/calculator (2.4ms)
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 ORDER BY `users`.`id` ASC LIMIT 1
  Rendered layouts/_calculator_script_top.html.erb (45.4ms)
  Rendered layouts/_calculator_header.html.erb (217.5ms)
  MenuItem Load (0.2ms)  SELECT `menu_items`.* FROM `menu_items` ORDER BY `menu_items`.`menu_priority` ASC
  Rendered layouts/_calculator_menu.html.erb (15.4ms)
  Rendered layouts/_calculator_script_bottom.html.erb (0.6ms)
Completed 200 OK in 325ms (Views: 295.8ms | ActiveRecord: 3.9ms)


Started GET "/en/custom_page/favicon.png" for 127.0.0.1 at 2014-05-12 11:50:35 +0530
Processing by CustomPageController#load_content as PNG
  Parameters: {"locale"=>"en", "name"=>"favicon"}
"---------------------"
"favicon"
Completed 500 Internal Server Error in 6ms
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_request.text.erb (3.3ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.6ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_session.text.erb (0.8ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.2ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_environment.text.erb (3.2ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_title.text.erb (0.2ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/_backtrace.text.erb (0.6ms)
  Rendered /home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/exception_notification-4.0.1/lib/exception_notifier/views/exception_notifier/exception_notification.text.erb (34.7ms)

An ActionView::MissingTemplate occurred in custom_page#load_content:

  Missing template custom_page/load_content with {:locale=>[:en], :formats=>[:png], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :arb]}. Searched in:
  * "/home/nithin/mobomo/Projects/sfth/app/views"
  * "/home/nithin/.rvm/gems/ruby-2.0.0-p353/bundler/gems/active_admin-c4a123d48850/app/views"
  * "/home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/kaminari-0.15.0/app/views"
  * "/home/nithin/.rvm/gems/ruby-2.0.0-p353/gems/devise-3.2.2/app/views"

Why is that favicon coming? How to prevent this from getting called? Processing by CustomPageController#load_content as PNG

Upvotes: 5

Views: 1157

Answers (3)

Uri Agassi
Uri Agassi

Reputation: 37409

Many browsers look for favicon.png files on your server (that is the icon shown near the title of the page). It is a known symptom seeing 404s for favicon.png in server logs.

The easiest way to avoid a browser looking at internal links for a favicon is to put a favicon.ico at the website root, or to add a <link rel to your layout template:

<link rel="shortcut icon" href="http://example.com/myicon.ico" />

Upvotes: 3

Fer
Fer

Reputation: 3347

Somehow the router is not realizing that that is an asset request.

Do you really need to define the route like that? if so, you could define the route like

get '/custom_page/:name' => 'custom_page#load_content', format: :html

So the request asking for a png format will not be routed to your controller.

Anyway I would try to use the resources method in the routes.rb:

resources :custom_page, only: :show

Note that this will use the standard rails naming, so you have to adjust a little your code:

First, rename the custom_page_controller.rb to custom_pages_controller.rb (controller names are, by convention, plural names) and second, you will be receiving the page name under the id param, instead of name.

So your method should look like this:

def load_content
  page_name = (params[:id]).split("_").join(" ")
  p "---------------------"
  p page_name
end

Regarding why you are receiving this request: The browser always tries to load the site's favicon (that is the tab's icon you see in almost all pages). I think if you don't provide the meta-tag for it, the browser tries to guess the url.

To have the meta-tag, you can use the favicon_link_tag helper in your layout's head and then have the image, usually called favicon.png, in your app/assets/images/ or in your public folder.

Upvotes: 3

Richard Peck
Richard Peck

Reputation: 76774

Firstly, why are you outputting directly in your action:

def load_content
    page_name = (params[:name]).split("_").join(" ")
    p "---------------------"
    p page_name
end

This needs to be as follows:

def load_content
    @page_name = (params[:name]).split("_").join(" ") #-> sends @page_name to the view, where you'll be able to render the `-------------`
end

Even if you're trying to render a png directly, I'd still advise against outputting directly from your controller. It's against MVC principles, and will make debugging & upgrading your app extremely difficult in future


Favicon

The favicon issue comes in here:

Started GET "/en/custom_page/favicon.png"

Your route consists of the following:

get '/custom_page/:name'

This means whenever you send a request to custom_page/_______.png, your controller will pick up the :name param & use that

As to why you're getting the favicon.png loaded, I can only guess as we've not got much to work from. You're getting your expected result, so the problem is likely in how this is being called / rendered

The two issues I could see would be:

  • Turbolinks
  • Layout

The first (turbolinks), would be a javascript issue. I don't know why it would render favicon.png, so I would recommend you test removing require turbolinks from your application.js file

The second (layout), is what my hunch tells me is the problem. If you're rendering a layout with this action, it would try and call the likes of favicon.png. To fix that, I would try using layout false in the controller to see if it fixes the issue

Hope this helps!

Upvotes: 3

Related Questions