Rails routes.rb assistance - ruby-on-rails

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.

Related

localhost:3000/topics/index and localhost:3000/topics/show BOTH route to same show.html file

For some reason both these URLS are routing to the same file when they shouldn't be, another thing that I noticed when typing in an invalid url such as localhost:3000/topics/inexjojvnsjg it just stays on the same page.
here is what my rails console is telling me when I try to access the url
localhost:3000/topics/index
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
Rendered topics/show.html.erb within layouts/application (0.1ms)
User Load (0.8ms) SELECT "users".* FROM "users" WHERE "users"."id" =$1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]]
Completed 200 OK in 98ms (Views: 96.5ms | ActiveRecord: 0.8ms)
here is my routes file....
Rails.application.routes.draw do
devise_for :users
get 'welcome/index'
get 'welcome/about'
# get "topics/index"
# get "topics/show"
# get "topics/new"
# get "topics/edit"
#for some reason, using resources:topics, index and show both route to show
resources :topics
root to: 'welcome#index'
post :incoming, to: 'incoming#create'
end
Here is the key info:
Started GET "/topics/index" for ::1 at 2015-02-06 17:33:07 -0700
Processing by TopicsController#show as HTML
Parameters: {"id"=>"index"}
The :index url for a TopicsController is "/topics".
The :show url for a TopicsController is "/topics/:id" or "/topics/1", where the last part of the url gets associated to the params[:id]. With the url "/topics/1" the :id = 1.
So when you go to the url "/topics/index" you are going to the :show action because of the "index" part of the url. You are just setting the :id to "index" instead of a Integer :id. You can see that in the output you pasted here:
Parameters: {"id"=>"index"}
TLDR: "/topics/index" is a route the will pass the Rails router but is an invalid route, because the :id is a String "index".

Rails: logger.debug not working inside Model function

I have a bunch of logger.debug statements in my Controller. They currently write the place where all the HTTP requests and everything are displayed in real time on my WEBRick server. However,
when I write logger.debug statements in my Model, they are not written to this same location, additionally they are not to be found in log/development which is the environment I have been working in. It is particularly frustrating because I make the call to the following Model function in a controller that has successfully executed logger.debug statements:
def process_payment
uri = URI('https://demo.myvirtualmerchant.com/VirtualMerchant/processxml.do')
the_xml = SchoolApplication.to_xml(params[:credit_card],
params[:expiration],
params[:cvv2_cvc2],
params[:amount],
params[:name])
logger.debug(the_xml)
the_xml = the_xml.to_s
my_hash = {'xmldata' => the_xml}
logger.debug(the_xml)
response = Net::HTTP.post_form(uri, my_hash)
#response_var = response
if response.response == 0
render 'receipt'
else
render 'card_Error'
end
end
the two logger.debug statements in this controller method are written to what I suppose is STDOUT? In any case I see them in the location which displays the following kind of information:
Rendered web_applications/card_Error.html.erb within layouts/application (1.7ms)
Completed 200 OK in 433ms (Views: 13.1ms | ActiveRecord: 0.0ms)
Started GET "/assets/jquery.js?body=1" for 127.0.0.1 at 2014-07-22 11:32:02 -0700
In the above Controller method when I make the call to SchoolApplication.to_xml,
it does not execute the corresponding logger.debug() statements (or maybe it writes them
to a different place? That wouldn't make sense but I am also a n00b, clearly) that are in
to_xml Model method as shown below:
def self.to_xml(number,expiration,cvv,amount, name)
xml = ::Builder::XmlMarkup.new
logger.debug(number)
logger.debug(name)
xml.txn {
xml.ssl_test_mode false
xml.ssl_card_number number
xml.ssl_amount amount
xml.ssl_cvv2cvc2_indicator cvv
xml.ssl_first_name name
xml.ssl_show_form false
xml.ssl_exp_date '25/34'
xml.ssl_track_data
xml.ssl_result_format HTML
xml.ssl_receipt_link_method
xml.ssl_receipt_link_url "#{Rails.root}" + '/receipts/receive'
xml.ssl_receipt_link_text
}
xml
end
N.B. I know that the above to_xml is being successfully called and returned from as the rest of my controller method which calls this model function executes without error.
Here is the output after I go to the route which calls the process_payment controller function:
Started POST "/application/pay" for 127.0.0.1 at 2014-07-22 12:13:40 -0700
Processing by WebApplicationsController#process_payment as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"pJD6Q7qJPXFNaGEczv5kTSKBC5TcG8lqhOuLNjw9UrY=", "web_application"=>{"first_name"=>"bob", "last_name"=>"job", "card_number"=>"41111111111111", "expiration_date"=>"232", "cvv2_cvc2"=>"232"}, "commit"=>"Save Web application"}
<txn><ssl_merchant_id/><ssl_user_id/><ssl_ssl_pin/><ssl_test_mode>false</ssl_test_mode> <ssl_card_number/><ssl_amount/><ssl_cvv2cvc2_indicator/><ssl_first_name/ ><ssl_show_form>false</ssl_show_form><ssl_exp_date>25/34</ssl_exp_date><ssl_track_data/><ssl_result_format>HTML</ssl_result_format><ssl_receipt_link_method/><ssl_receipt_link_url> <ssl_receipt_link_text/></txn><to_s/>
<txn><ssl_merchant_id/><ssl_user_id/><ssl_ssl_pin/><ssl_test_mode>false</ssl_test_mode> <ssl_card_number/><ssl_amount/><ssl_cvv2cvc2_indicator/><ssl_first_name/><ssl_show_form>false</ssl_show_form><ssl_exp_date>25/34</ssl_exp_date><ssl_track_data/><ssl_result_format>HTML</ssl_result_format><ssl_receipt_link_method/>
Rendered web_applications/card_Error.html.erb within layouts/application (3.8ms)
Completed 200 OK in 742ms (Views: 22.6ms | ActiveRecord: 0.0ms)
which is equivalent to what was written in development.log

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
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.

Select_Tag not working in rails?

Here is my code for select_tag
<%= select_tag :tp, options_for_select([["movable",1],["fixed",2]]), :prompt => "select type of asset" %>
it works if i replace by:
<%= f.text_area :tp %>
If this in not enough i will give more code as you desire:
here is the log after i use select_tag
Started POST "/assets" for 127.0.0.1 at 2014-03-07 23:40:34 +0530
Processing by AssetsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Gib6rSODjXgXLZ9+5sDcq0ZatkA7144hU+Em2X7KONU=", "asset"=>{"name"=>"", "location"=>"", "cost"=>""}, "tp"=>"1","commit"=>"Save"}
(1.0ms) BEGIN
(1.0ms) ROLLBACK
Redirected to http://localhost:3000new
Completed 302 Found in 5ms (ActiveRecord: 2.0ms)
[2014-03-07 23:40:34] ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1203:in `rescue in merge'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/uri/generic.rb:1200:in `merge'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:275:in `setup_header'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpresponse.rb:205:in `send_response'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/httpserver.rb:110:in'run'
F:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'
Started GET "/assets/logo.png" for 127.0.0.1 at 2014-03-07 23:40:36 +0530
Error after changing to `real_assets_path
Started GET "/real_assets/new" for 127.0.0.1 at 2014-03-09 13:56:47 +0530
ActiveRecord::SchemaMigrationLoad(1.0ms)SELECT"schema_migrations".* FROM"schema_migrations"
Processing by AssetsController#new as HTML
Rendered assets/new.html.erb within layouts/application (30.0ms)
Completed 500 Internal Server Error in 98ms
ActionView::Template::Error (undefined method `assets_path' for #<# <Class:0x545bb78>:0x545aeb8>):
1: <div class = "container">
2: <div id = "assetnew">
3: <%= form_for(#asset) do |f| %>
4: <center><h1>Add Asset</h1></center>
5: <div class = "well">
6: <a class ="red">
app/views/assets/new.html.erb:3:in`_app_views_assets_new_html_erb___457532114_11608392'
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.0ms)
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)
Rendered
F:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/actionpack-4.0.0/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (18.0ms)
Assets
This is the error Rails is returning:
ERROR URI::InvalidURIError: the scheme http does not accept registry part: localhost:3000new (or bad hostname?)
Without seeing your form, it seems you've got an issue with calling your resource /assets (as mentioned in the comments)
Basically, Rails treats the assets pipeline as a base directory - localhost:3000/your_asset_file.png -- it looks like this is what's happening here
Because your app is referencing /assets, I would surmise it's confusing it with the assets dir. Try doing this:
#config/routes.rb
resources :assets, as: "real_assets", path: "real_assets" #-> real_assets_path - /real_assets
Select
The select problem is likely caused by your assets path problem
select_tag is a standalone helper method (doesn't need any FormBuilder to help it). This means you should be able to just use it in an .html.erb file without any problems
The syntax for the select looks okay, so I'd recommend if you get your assets path fixed, you should be able to use it

routes lead to blank view, no html

I have a very similar issue to this question: My route returns a blank view (no html when I do view source)
When I access a route, it just shows a blank view with no html when viewing source.
My route returns a blank view (no html when I do view source)
config/routes.rb
SampleApp::Application.routes.draw do
devise_for :users
root to: 'home#index'
end
app/controllers/home_controller.rb
class HomeController < ApplicationController
def index
end
end
app/views/home/index.html.slim (slim is like erb, I tested both out but still no html rendered)
h1 get ready to party!
log/development.log
Started GET "/" for 127.0.0.1 at 2012-12-05 00:36:21 -0500
Processing by HomeController#index as */*
Rendered home/index.html.slim within layouts/application (0.4ms)
Completed 200 OK in 6ms (Views: 5.9ms | ActiveRecord: 0.0ms)
The other question suggested changing ports. I did all the basic stuff like restart server, restart computer, try different ports.
It sounds like your layout could be blank and is not rendering the index view.
The default layout is application.html.erb and should render your views with <%= yield %>.
Also - have you tried restarting your server for kicks? That's the answer to the other SO question you posted so I'm assuming you've tried that...

Resources