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
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" />
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!
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.
Related
thanks in advance for your help!!
I have in routes.rb:
get 'api/streets:name' => 'streets#get_by_name', as: "get_by_name"
I have in streets_controller.rb:
ids = params[:name]
I have in Javascript:
const params = encodeURI('name[]=1&name[]=2')
fetch(`/api/streets?${params}`)
When I call the api from the front end, I get the following log message:
Started GET "/api/streets?name%5B%5D=2&name%5B%5D=5" for 127.0.0.1 at 2019-09-06 17:10:59 -0700
Rendering pages/index.html.erb within layouts/application
Processing by PagesController#index as */*
Parameters: {"name"=>["2", "5"], "path"=>"api/streets"}
Rendered pages/index.html.erb within layouts/application (7.1ms)
Rendering pages/index.html.erb within layouts/application
Why is it using the PagesController and not the StreetsController?
In all other cases where I'm getting and posting and putting and deleting on the api, the router knows what controller to use. It's just this one case when I'm using array parameters where it's routing to the wrong controller.
It's probably just something dumb I've done.
Your route expects a path like: /api/streets123 - where params[:name] would be equal to "123" and no, that's not a typo.
You should just use:
get 'api/streets' => 'streets#get_by_name', as: "get_by_name"
If you need to enforce the existence of the :name parameter then you should use the :constraints option.
I have a simple photo gallery on a site, and am having some trouble when I invoke either the edit or delete paths. I click on the button and the page acts like it is going to load, then goes to an error page. Here is the trace from the console:
Started GET "/photos/17/edit" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
Started GET "/photos/17/edit" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
Processing by PhotosController#edit as HTML
Processing by PhotosController#edit as HTML
Parameters: {"id"=>"17"}
Parameters: {"id"=>"17"}
Photo Load (0.2ms) SELECT "photos".* FROM "photos" WHERE "photos"."id" = $1 LIMIT 1 [["id", 17]]
Photo Load (0.2ms) SELECT "photos".* FROM "photos" WHERE "photos"."id" = $1 LIMIT 1 [["id", 17]]
Album Load (7.9ms) SELECT "albums".* FROM "albums"
Album Load (7.9ms) SELECT "albums".* FROM "albums"
Rendered photos/_form.haml (35.7ms)
Rendered photos/_form.haml (35.7ms)
Rendered photos/edit.haml within layouts/application (38.9ms)
Rendered photos/edit.haml within layouts/application (38.9ms)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Rendered layouts/_navbar.html.haml (5.0ms)
Rendered layouts/_navbar.html.haml (5.0ms)
Completed 200 OK in 187ms (Views: 174.4ms | ActiveRecord: 9.4ms)
Completed 200 OK in 187ms (Views: 174.4ms | ActiveRecord: 9.4ms)
Then I get this immediately after, before the edit page loads:
Started GET "/undefined" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
Started GET "/undefined" for 127.0.0.1 at 2015-10-12 09:41:08 -0700
ActionController::RoutingError (No route matches [GET] "/undefined"):
I've looked over my controller and methods and haven't seen anything that would suggest the need for it to look for the /undefined path. What is worse is that it doesn't happen every time, which made me think there was something wrong with my ruby installation or something. If that was the case, it would not give the error on Heroku (which it does) and would probably error out in other applications I work on (which it doesn't).
Controller:
def update
respond_to do |format|
if #photo.update(photo_params)
format.html { redirect_to #photo, notice: 'Photo was successfully updated.' }
format.json { render :show, status: :ok, location: #photo }
else
format.html { render :edit }
format.json { render json: #photo.errors, status: :unprocessable_entity }
end
end
end
def destroy
#photo.destroy
respond_to do |format|
format.html { redirect_to photos_path, notice: 'Photo was successfully destroyed.' }
format.json { head :no_content }
end
end
Model:
class Photo < ActiveRecord::Base
mount_uploader :image, ImageUploader
belongs_to :album
validates_presence_of :image
end
When I notice that it is redirecting me, I can just browse to the picture itself fine (photos/8), but the edit path is still kicking me to the /undefined (photos/8/edit).
I realise (and hope) you might have solved this already, but I've been banging my head against the wall due to a similar problem - a GET request to /undefined route. Luckily, I was able to solve it and this might help people with similar problems.
To explain my situation - I have some AJAX calls and I suspected it might have something to do with that, even though I did not build up any URLs with variables that could be undefined.. or so I thought (I was partially right, though).
I was fetching some information (including image URLs to be rendered in view thereafter) based on data returned by an API in JSON format. I hope you see where I am going with this :)
Turns out one image (for that specific API request, I am sure there are other such cases which I haven't yet found) had no URL defined. As I passed the json data to a constructor, I expected the image URL to be defined and inserted that into the image src attribute. The URL was, however, apparently undefined. Therefore, instead of loading the image via absolute path to the API's CDN, as the src had no http(s)://, it tried to load it from local server. As the variable with image url was undefined, the image src attribute pointed to a relative /undefined route. Hence a GET request to "/undefined" route.
I should note that this was not an ajax call per se, the get request was a result of using data from ajax call to construct an image src attribute. The fact that I used that data for constructing a google maps infowindow HTML structure (which included an image in it) only compounded the problem as the missing image (due to undefined src) would only show up after clicking on a specific google maps marker.
So, for your case (while it might not be relevant for you anymore, might be for someone else) - make sure you look at image src attributes as images will start loading immediately after your page (or in case of ajax loading and constructing HTML image tags afterwards) loads. Hence one 'normal' request that gets processed as it should. If, however, an image src attribute is "undefined", it will then immediately start a get request to "/undefined" route.
I get this in my server log, which seems to indicate that it returned ajaxresponse.js.erb:
Started GET "/ajaxresponse" for 70.28.21.25 at 2013-10-01 15:38:54 +0000
Processing by ContentController#ajaxresponse as JS
Rendered content/ajaxresponse.js.erb within layouts/content (0.5ms)
Rendered inc/_analytics.erb (0.3ms)
Completed 200 OK in 46ms (Views: 45.3ms | ActiveRecord: 0.0ms)
Inside ajaxresponse.js.erb :
$('#ajaxdiv').append("fdsa");
There is a div on the page with ID "ajaxdiv"
but it doesn't seem to change anything? I've tried 100 different javascript commands within the .js.erb file, but none of them are affecting the page.
Controller action:
def ajaxresponse
respond_to do |format|
format.js {}
end
end
Chrome console log :
Uncaught Error: jquery-ujs has already been loaded!
Edit: Just remoted jquery-ujs from the application.rb file and there are no more errors. The page, however, still doesn't update with ajax
The ajaxdiv is below the save button and has default text: "ohai"
To get a div with the id "ajaxdiv", you need to use #ajaxdiv, not ajaxdiv:
$('#ajaxdiv').append("fdsa");
I am using Jbuilder (and I also tried to use Rabl) to render json.
When I try to render the jbuilder template in my application it renders
the template within the layouts/application file and returns HTML as JSON (see line 'within layouts/application'):
Rides controller on Github
Started GET "/random_photo.json"
Processing by RidesController#random_photo as JSON
>> Rendered rides/random_photo.json.jbuilder within layouts/application (0.3ms)
Rendered shared/_banners_in_corners.haml (3.0ms)
Rendered shared/_sign_in_and_out.haml (2.0ms)
Rendered layouts/_navigation.haml (7.3ms)
Completed 200 OK in 156ms (Views: 120.7ms | ActiveRecord: 3.1ms)
However, when I render the json without a template, and do a render json: #ride.as_json call, things work as expected.
When I create a new application and I try to do the same thing, everything works as expected as well:
Started GET "/posts/1.json"
Processing by PostsController#show as JSON
Post Load (0.1ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = ? LIMIT 1 [["id", "1"]]
Rendered posts/show.json.jbuilder (0.6ms)
Completed 200 OK in 20ms (Views: 19.2ms | ActiveRecord: 0.1ms)
I don't know what I have done with my application that it is not rendering the
templates correctly. Any ideas?
Gosh, it took me about two days to find out that the issue that is causing this problem is
my layouts/application file which was named aplication.haml.
Now when I wanted to use a template engine like Builder for xml or Rabl for json, it tried to render the template within the yield field in the layouts/application.haml file and thus returning html as JSON
I found out that the problem lies within the application.haml file
naming it application.html.haml solved the problem...
I've tried looking at other answers for this but I can't seem to figure out why my redirect isn't working.
So I'm using Devise with Rails 3.1, and I'm making a shopping site. Visitors aren't allowed to add things to their cart unless they are signed in. This is what I'm having trouble with: if they aren't signed in, I want to redirect them to the Items index page. Here's what I have:
class ItemsController < ApplicationController
def add_to_cart
#item = Item.find(params[:id])
if current_user
#item.update_attributes(:cart_id => #current_cart.id)
redirect_to :back
else
redirect_to categories_path, notice: 'You must sign in to add an item to your cart.'
end
end
.
.
.
end
As of right now, when I click the link to add to cart, the method gets executed (I can see Rails loading and defining #item in the server log), and it reaches the 'else' statement, but no redirect happens.
I've already generated scaffolding for the index, new, etc. (all the RESTful actions). Also, I'm sure that I'm reaching the add_to_cart method because I've tried debugging with some puts statements. What's happening here?
EDIT:
Also, another weird thing which may be of use... The server seems to try to execute this method twice, and tries to 'get' categories twice:
Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.3ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 26ms
Started GET "/items/3/add_to_cart" for 127.0.0.1 at 2012-01-12 16:53:11 -0800
Processing by ItemsController#add_to_cart as JS
Parameters: {"id"=>"3"}
Category Load (0.2ms) SELECT "categories".* FROM "categories"
Item Load (0.2ms) SELECT "items".* FROM "items" WHERE "items"."id" = $1 LIMIT 1 [["id", "3"]]
Redirected to http://localhost:3000/categories
Completed 302 Found in 25ms
Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 35ms (Views: 28.5ms | ActiveRecord: 4.2ms)
Started GET "/categories" for 127.0.0.1 at 2012-01-12 16:53:12 -0800
Processing by CategoriesController#index as JS
Category Load (0.2ms) SELECT "categories".* FROM "categories"
CACHE (0.0ms) SELECT "categories".* FROM "categories"
Rendered categories/index.html.erb within layouts/application (0.0ms)
Completed 200 OK in 37ms (Views: 30.6ms | ActiveRecord: 4.2ms)
EDIT 2 (as requested by Delba)
resources :items do
member do
get 'add_to_cart'
end
end
EDIT 3: changing the else statement to respond to javascript
respond_to do |format|
format.js { redirect_to items_path, notice: 'You must sign in to add an item to your cart.' }
end
For anyone who may need answers to this question, simply replace redirect_to statements with the following:
respond_to do |format|
format.js
end
Then, in your views under items, make a add_to_cart.js.erb page, consisting of javascript to make notices, or do whatever. Here's what I put in mine:
alert("Need to be signed in")
EDIT: Also, for the part where it executes twice: this is somewhat unrelated, but for some reason by default Rails includes duplicate Javascripts. Specifically, look at application.js: it says require jquery and require jquery_ujs. Disable one of these and you're home free.
To disable one of these javascripts:
Go to assets/application.js
Remove the comments (the // ) before require jquery, require tree .
This way, Rails doesn't assume the default and instead includes only jquery and whatever other javascripts you have in assets/javascripts