Get current path with name of query params in the path - ruby-on-rails

In a Rails controller, how can I access my current path, but with the query params in the same format as what we have in rails routes ?
So for example, in www.test.com/record/3 the helper I'm looking for would return record/:id.
I've tried request.path but it does not return the name of the param in the URL. It returns the actual path, which is not what I want.

What you're looking for are called dynamic segments (or sometimes named placeholders).
You can get the them programatically for a given request with request.path_parameters.except(:action, :controller).
# route is defined as 'get /:foo/:bar'
irb(main):013:0> app.get '/a/b?x=2'
Started GET "/a/b?x=2" for 127.0.0.1 at 2023-01-11 01:08:53 +0100
Processing by PagesController#home as HTML
Parameters: {"x"=>"2", "foo"=>"a", "bar"=>"b"}
Rendering layout layouts/application.html.erb
Rendering pages/home.html.erb within layouts/application
Rendered pages/home.html.erb within layouts/application (Duration: 0.1ms | Allocations: 7)
Rendered layout layouts/application.html.erb (Duration: 2.7ms | Allocations: 2363)
Completed 200 OK in 4ms (Views: 3.3ms | Allocations: 2675)
=> 200
irb(main):014:0> app.request.path_parameters
=> {:controller=>"pages", :action=>"home", :foo=>"a", :bar=>"b"}
Unlike the params object this doesn't contain parameters from the query string or request body.

Related

Can't read local geojson

Trying to read a geojson in Rail. Specifically
/public/switzerland.geojson
Console error:
Started GET "/oladvancedview" for ::1 at 2023-02-02 16:42:44 -0800
Processing by DemoController#oladvancedview as HTML
Rendering layout layouts/application.html.erb
Rendering demo/oladvancedview.html.erb within layouts/application
Rendered demo/oladvancedview.html.erb within layouts/application (Duration: 0.3ms | Allocations: 160)
Rendered layout layouts/application.html.erb (Duration: 3.0ms | Allocations: 2803)
Completed 200 OK in 4ms (Views: 3.7ms | Allocations: 3039)
Started GET "/Users/gscar/Documents/Ruby/RailsTrials/os-stimulus-mapping/public/switzerland.geojson" for ::1 at 2023-02-02 16:42:44 -0800
ActionController::RoutingError (No route matches [GET] "/Users/gscar/Documents/Ruby/RailsTrials/os-stimulus-mapping/public/switzerland.geojson"):
rails routes
public_switzerland GET /public/switzerland(.:format) public#switzerland {:format=>"json"}
routes.rb get 'public/switzerland', defaults: { format: 'json' }
xx.html.erb <% path = File.join Rails.root, 'public', 'switzerland.geojson' %
Passing with Stimulus
<div id="avmap"
data-controller="ol-advanced-view"
data-ol-advanced-view-target="avmap"
data-ol-advanced-view-url-value = "<%= path %>"
style="height:400px" >
</div>
No Javascript errors in stimulus controller.
Clueless what is going on. How do I read this file? Thank you for any ideas?

rails implementing ransack search - routing configuration

Fairly new rails developer, and still trying to get my head around where things go and how to connect them.
I have a database of 'records', and am looking to search them. I found the ransack gem, which does this, however I don't want to put the search on the index page, I want a seperate page for the search and it's results.
I created a new action in the records controller:
def search
#q = Record.ransack(params[:q])
#found_records = #q.result(distinct: true)
end
and then the search.html.erb view, then the route:
resources :records do
match :search, to: 'records#search', on: :collection, via: [:get, :post]
end
and then the view itself
<%= search_form_for(
#q,
url: search_records_path,
html: { method: :post }
) do |f| %>
<%= f.label :brief %>
<%= f.search_field :brief %>
<%= f.submit %>
<% end %>
<div id="records">
<% #found_records.each do |record| %>
<%= render record %>
<% end %>
</div>
and this runs without errors, but when I press the search box the page just refreshes, with no search performed.
I guess this is a routing issue, but now sure how to set the route used by the search button? Any advice here much appreciated!
--edit
The log looks good to me, here is what is logged on the console.
Started POST "/records/search" for 127.0.0.1 at 2022-08-09 05:35:52 +0800
Processing by RecordsController#search as HTML
Parameters: {"authenticity_token"=>"[FILTERED]", "q"=>{"brief"=>"rain"}, "commit"=>"Search"}
Rendering layout layouts/application.html.erb
Rendering records/search.html.erb within layouts/application
Record Load (0.1ms) SELECT DISTINCT "records".* FROM "records"
↳ app/views/records/search.html.erb:20
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 49)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 47)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 48)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 47)
Rendered records/_record.html.erb (Duration: 0.1ms | Allocations: 49)
Rendered records/search.html.erb within layouts/application (Duration: 4.5ms | Allocations: 1984)
Rendered layouts/_shim.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layouts/_header.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layouts/_footer.html.erb (Duration: 0.1ms | Allocations: 15)
Rendered layout layouts/application.html.erb (Duration: 24.0ms | Allocations: 7469)
Completed 200 OK in 26ms (Views: 24.7ms | ActiveRecord: 0.1ms | Allocations: 8216)
-- update --
Changing the search to a 'get' fixed the issue, and making sure to use #q and not a custom name without changing the ransack config to match.

Rails Receives Repetitive Requests on One Request

My route is as follows:
Rails.application.routes.draw do
get 'courses' => 'application#index'
get 'check_db' => 'application#check_db'
root 'application#index'
end
And my application#check_db is
def check_db
p "test"
redirect_to root_path
end
When I visit /check_db on my browswer (tried both in Chrome and Safari), if working properly, it should GET "/check_db" and then GET "/". But actually, the log shows that there are four requests, with the above pattern repeated twice (i.e. GET "/check_db", GET "/", GET "/check_db", GET "/"). There is absolutely no code in my application#index that does any other redirection. So why is this?
The situation described above happen most of the times. However, occasionally, it works as expected. I did not change the code in the interim.
If it is in interest, the entire log is pasted below:
Started GET "/check_db" for ::1 at 2017-01-04 17:05:06 -0800
Processing by ApplicationController#check_db as HTML
"test"
Redirected to http://localhost:3000/
Completed 302 Found in 1ms
Started GET "/" for ::1 at 2017-01-04 17:05:06 -0800
Processing by ApplicationController#index as HTML
Rendering application/index.html.erb within layouts/application
[MongoDB query log]
Rendered application/index.html.erb within layouts/application (3.4ms)
Completed 200 OK in 18ms (Views: 16.4ms)
Started GET "/check_db" for ::1 at 2017-01-04 17:05:07 -0800
Processing by ApplicationController#check_db as HTML
"test"
Redirected to http://localhost:3000/
Completed 302 Found in 8ms
Started GET "/" for ::1 at 2017-01-04 17:05:07 -0800
Processing by ApplicationController#index as HTML
Rendering application/index.html.erb within layouts/application
[MongoDB query log]
Rendered application/index.html.erb within layouts/application (4.1ms)
Completed 200 OK in 17ms (Views: 15.0ms)

Double token check in devise

Devise is acting weird for the first time each day when a User tries to create a new User in our project. This is the server log from the exact moment the user clicks on the confirm link:
Started GET "/users/confirmation?confirmation_token=3F367iMzUm5y3ohbkykM" for 199.116.169.254 at 2014-07-22 15:34:29 +0000
Processing by Devise::ConfirmationsController#show as */*
Parameters: {"confirmation_token"=>"3F367iMzUm5y3ohbkykM"}
Redirected to http://qa.nearshoreconnect.com/users/sign_in
Completed 302 Found in 228ms (ActiveRecord: 2.2ms)
Started GET "/users/sign_in" for 199.116.169.254 at 2014-07-22 15:34:29 +0000
Processing by Devise::SessionsController#new as */*
Rendered layouts/_errors.html.erb (0.1ms)
Rendered devise/sessions/new.html.erb within layouts/application (6.1ms)
Rendered layouts/_shim.html.erb (0.4ms)
Rendered layouts/_search_box.html.erb (1.2ms)
Rendered layouts/_header_internet.html.erb (9.6ms)
Rendered layouts/_footer_internet.html.erb (1.4ms)
Completed 200 OK in 29ms (Views: 27.5ms | ActiveRecord: 0.0ms | Solr: 0.0ms)
Started GET "/users/confirmation?confirmation_token=3F367iMzUm5y3ohbkykM" for 201.234.55.21 at 2014-07-22 15:34:30 +0000
Processing by Devise::ConfirmationsController#show as HTML
Parameters: {"confirmation_token"=>"3F367iMzUm5y3ohbkykM"}
Rendered layouts/_errors.html.erb (0.7ms)
Rendered devise/confirmations/new.html.erb within layouts/application (3.9ms)
Rendered layouts/_shim.html.erb (0.4ms)
Rendered layouts/_search_box.html.erb (1.0ms)
Rendered layouts/_header_internet.html.erb (2.6ms)
Rendered layouts/_footer_internet.html.erb (1.3ms)
Completed 200 OK in 18ms (Views: 14.1ms | ActiveRecord: 0.3ms | Solr: 0.0ms)
Started GET "/assets/font.css" for 201.234.55.21 at 2014-07-22 15:34:30 +0000
Served asset /font.css - 304 Not Modified (0ms)
The validation token is checked twice. I'm overriding the confirm function in my User model. Is that the problem? The code:
def confirm!
super
send_welcome_mail
end

authenticate_user! hijacking the registrations/create method

The problem I'm having seems to be that Devise's authenticate_#{role}! method is hijacking my registration attempt.
Started GET "/client/sign_up" for 127.0.0.1 at 2012-01-14 12:02:52 +0000
Processing by Client::RegistrationsController#new as HTML
Rendered /Users/robertwwhite/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views/devise/shared/_links.erb (1.4ms)
Rendered client/registrations/new.html.haml within layouts/application (97.6ms)
Rendered client/_navigation.html.haml (1.6ms)
Rendered shared/_flash_messages.html.haml (0.1ms)
Completed 200 OK in 126ms (Views: 116.4ms | ActiveRecord: 7.2ms)
Started POST "/client" for 127.0.0.1 at 2012-01-14 12:02:58 +0000
Processing by WishesController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"vq3wgsQeb4eoxhb3sw2Q2kd4edIoOxIfrzJ/WzJUAn0=", "client"=>{"email"=>"bacon#example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}
Completed 401 Unauthorized in 13ms
Started GET "/client/sign_in" for 127.0.0.1 at 2012-01-14 12:02:58 +0000
Processing by Client::SessionsController#new as HTML
Rendered /Users/robertwwhite/.rvm/gems/ruby-1.9.2-p290/gems/devise-1.5.3/app/views/devise/shared/_links.erb (1.0ms)
Rendered client/sessions/new.html.haml within layouts/application (16.5ms)
Rendered client/_navigation.html.haml (1.5ms)
Rendered shared/_flash_messages.html.haml (0.3ms)
Completed 200 OK in 60ms (Views: 38.6ms | ActiveRecord: 6.4ms)
I've tried overriding the after_signup_path_for(resource_or_scope) but it seems to be getting ignored.
# app/controllers/application_controller.rb
def after_sign_up_path_for(resource_or_scope)
random_path
end
So as it stands users can't register to the site in the first place. Any ideas?
Have you checked to make sure non of your routes are overriding the default devise routes/methods?
Edited by HaaR for clarity of users with similar problem:
I had the following in my config/routes.rb above my devise_for methods.
match "client" => "wishes#index"
Which was overriding Devise's
devise_for :clients, :path => :client
By moving it below, it gives Devise priority, and still passes the get request to the appropriate controller and action without hijacking the POST requests.

Resources