How to get a query string from a URL in Rails
If you have a URL in a string then use URI and CGI to pull it apart:
url = 'http://www.example.com?id=4&empid=6'
uri = URI.parse(url)
params = CGI.parse(uri.query)
# params is now {"id"=>["4"], "empid"=>["6"]}
id = params['id'].first
# id is now "4"
Please use the standard libraries for this stuff, don't try and do it yourself with regular expressions.
Also see Quv's comment about Rack::Utils.parse_query
below.
References:
CGI.parse
URI.parse
Update: These days I'd probably be using Addressable::Uri
instead of URI
from the standard library:
url = Addressable::URI.parse('http://www.example.com?id=4&empid=6')
url.query_values # {"id"=>"4", "empid"=>"6"}
id = url.query_values['id'] # "4"
empid = url.query_values['empid'] # "6"
Get query string from extract URL if exists
The keys in query string in a URL can be accessed using params[]
hash, even if the URL is not present inside the app itself. This is how it is convenient for rails to communicate with the outside world as well. Note that rails treats all the HTTP verbs equally when it comes to usage of params[]
hash.
Usage: puts params[:your_key]
Rails 5 Query from url address with params
As Ray said, if you add:
<%= link_to articles_path(author: "name_here") %>
that will generate the url http://localhost:3000/articles?author=name_here
You may find it easier to use a gem to easily implement a search for articles by author name but if you just want to do this manually in your ArticlesController:
def index
if params[:author]
@author = params[:author]
@articles = Article.all.where(author: @author)
else
@articles = Article.all
end
end
Query Parameters in URL from a Rails form
As you're not creating anything I would recommend using GET
requests with query string parameters. So the query can be shared with the url.
Url for your example image would be something like:
http://www.yourwebsite.com/?max_price=5
Which gives you a params[:max_price]
in controller.
Get only the query string in current view
To just get the query string for your request, request.query_string
returns everything but the '?'
Get current action's path/url including query string? (Rails)
request.url
is probably what you are looking for.
Create url query params and pass to Rails controller
When you are executing window.open(url, "_self");
, it is requesting as a html get request. Why don't you use ajax?
$.ajax({
url: url,
method: "GET",
dataType: "script", /*optional*/
success: function(response){
console.log(response)
}
})
Option 2:
$("a").on("click", function(){
$.ajax({
url: "http://www.myapp/todos/data",
type: "get",
dataType: "script",
data: {
make_id: make,
body_id: body
},
success: function(response) {
console.log(response);
},
error: function(xhr) {
//Do Something to handle error
}
});
return false;
})
in the place of $("a")
, use your desired selector.
How to extract URL parameters from a URL with Ruby or Rails?
I think you want to turn any given URL string into a HASH?
You can try http://www.ruby-doc.org/stdlib/libdoc/cgi/rdoc/classes/CGI.html#M000075
require 'cgi'
CGI::parse('param1=value1¶m2=value2¶m3=value3')
returns
{"param1"=>["value1"], "param2"=>["value2"], "param3"=>["value3"]}
Rails query string in URL?
You just need define a form with get method instead of post
<% form_tag search_url, :method => :get do %>
<%=text_field_tag :search %>
<%= submit_tag %>
<% end %>
rails seems to delete query params in URL with 'button_to' helper and 'get' method
I suggest you add params: { }
at the end of button_to
inside the braces that hold http_options, it will generate hidden_field
which contains the value.
params: { :delete_id => saved_search.id }
Related Topics
No Such File to Load - Rubygems (Loaderror)
Your Ruby Version Is 2.0.0, But Your Gemfile Specified 2.1.0
How to Use Ruby to Colorize the Text Output to a Terminal
In Ruby on Rails, How to Format a Date With the "Th" Suffix, as In, "Sun Oct 5Th"
How to Create a Deep Copy of an Object in Ruby
Is There a "Do ... While" Loop in Ruby
How to Install Postgresql'S Pg Gem on Ubuntu
How to Find Which Operating System My Ruby Program Is Running On
Nokogiri/Xpath Namespace Query
Usage of Attr_Accessor in Rails
How to Avoid "Cannot Load Such File - Utils/Popen" from Homebrew on Osx