Autofill form based on URL string value in Rails - ruby-on-rails

I have a website with a form for location on the front page. I want to auto-populate the form with values based on a URL string. How can I do this in rails?
For example, I would like the form to show with the value New York when the user arrives on the front page based on something like: website.com?city="New York"
How best to implement this in rails?

First of all, you can get the current request url, everything is here : How do I get the current absolute URL in Ruby on Rails?
Then you just have to "parse" it :
<% city = request.fullpath.split('=').last %>
Will return "New York" with a url like website.com?city="New York"
(Not sure about this one but maybe you can get it only with params[:city], I just don't think params is available in a view)
The rest depends on the structure of your form...

Related

Cities Text Field with Auto Complete in Rails

I've searched a little about some ways to add a second field after States, to set a City based on it. I saw two options first : observe_field (From prototype) and jQuery, both with a select box. The fact is that I don't know JavaScript, and am in a hurry to set this... Well, I thought a text field with auto completion would be better, and maybe easiest to do.
Here we go : I have an model Uf and City, and both is populated with data. City has a "belongs_to Uf". The question is : How can I set something like this? Is there a way to do this with only ruby and rails?
To do this you'll want to have an AJAX action that responds to some search string query sent each time the user enters text. This action would respond to the view with data containing the result set of the search.
Here's a very simple example:
City Controller:
def search
term = params[:term]
cities = City.where('name LIKE ? OR code LIKE ?', "%#{term}%", "%#{term}%").order(:name)
render json: cities.map { |city|
{
id: city.id,
label: city.inspect,
value: city.full_name
}
}
end
Upon receiving a response, the dropdown would be populated with the results of the search. Selecting a value would set the field and store the matching id for the city in a hidden field for when the form is submitted.
I'd consider exploring the jQuery autocomplete Gem. If you're using Rails, there's a full-stack version here https://github.com/bigtunacan/rails-jquery-autocomplete which should be good to learn from.

Parsing params from url to next page in rails

I currently have a landing page /vouchers and then many pages linked to that for example /vouchers/1, /vouchers/2 etc.
I currently have the functionality that if this is entered /vouchers/1?offer=50 then the form on that page is sent with the offer included in the url.
Now I need to add the functionality so if /vouchers?offer=50 is entered, any page that is then navigated to, will keep these parameters. (for example /vouchers/1 should then include the offer from the url)
I see only one way.
store all "special" parameters in session
in any handler — if "special" parameters are in session — form new url with this parameters and redirect user there
Let me guess :
1. you have a model , named "Voucher"
2. In case you want to see the entry for , let's say , voucher#1 , you see URL voucher/1 (notice the singular form of the word 'voucher')
3. you assign the value of the offer by some interaction by user , for example - link or list . Let's say it's a link . Try this (HAML):
= link_to "Accept this offer and create a new voucher", new_voucher_path(:offer => #offer_value)
(in your case #offer_value = 50)
The following view (or partial ) will be able to read the value in :offer .
You can refer to this useful discusion .
I solved this, probably not in the best way. On the /voucher page, I added if statements to all the links, so if there's an offer in the url, the links are appended appropriately.

How to retrieve the URL of the current browsed Web page in my application and to add to it some query parameter?

I am using Ruby on Rails 3.2.2 and I would like to know how to retrieve the URL of the current browsed Web page in my application and to add to it some query parameter. That is, given a user is browsing the page http://www.my_application_name.org/articles/2, I would like to "build" something like link_to http://www.my_application_name.org/articles/2?param1=abc&param2=efg.
How can / should / could I make that?
You can use the url_for method and pass a ruby hash to add query parameters to current URL. For example,
<%= link_to 'Current URL with query params', url_for(param1: 'abc', param2: 'efg') %>
That should work.
If you want to get the current URL with host, port etc., you can use the request object to construct one as shown below.
<%= "http://#{request.host}:#{request.port.to_s+request.fullpath}" %>

Multi-step form using GET requests

We have the unusual requirement of a multi-step form through GET requests. So, instead of POSTing the forms, we're using GET requests to pass new parameters to the query. So, the basic idea is that there's a treatment, and a date passed to the query. A three-step form if you will.
Show available treatments, pick one
Show available dates (there's business logic in the background that figures these out)
Pick a time
The URL will go through the following states
site.com/bookings/new
site.com/bookings/new/[id|name_of_treatment] (by this, I mean it could either by the ID field or the name of the the treatment)
site.com/bookings/new/[id|name_of_treatment]/2010-12-12/
So, my route looks like this:
map.connect 'bookings/new/:massage_type/:date', :controller => :bookings, :action => :new
massage_type is synonymous with the treatment_id.
So my form_tag looks like this:
<% form_tag( {:action => "new"}, :method => "get" ) do %>
The problem I'm having is that I want it to just give me back the URL site.com/bookings/new/[id|name_of_treatment]/ but instead it gives back the following URL:
http://localhost:3000/bookings/new?massage_type[treatment_id]=1&commit=actionnew
I don't suppose anyone knows?
Forms that use GET are adding the input values as query parameters. There's no way to make the form post to a different URL, where the input values are part of the URL instead - it's just not supported by the HTML standard.
You could use URL rewrite to remap the incoming URLs of this type to the one you want, however that's not really a good solution, because this would result in a second request.
However, what I don't understand is why does the form need to do GET to that specific URL. Is it a requirement that these URLs can be constructed by the user manually, instead of using the form?
If there is no such requirement, I would advise to use standard POST form to http://localhost:3000/bookings/new and modify the form in the response based on the parameters in the POST body, as necessary.
Better yet, write some Ajax that would update the form according to the user's choice, without making a full form submit, until the user has finished all the choices.
By definition, the result of a GET request will have a query string (?param1=value1&param2=value2&...) in its URL. To get rid of those, you'll have to either start using POST or immediately redirect to the desired URL upon receiving a GET request.
I rather like the redirect approach because it doesn't show that confusing/annoying message about resubmitting POST data when the user refreshes their browser.

How to implement a search page which shows results on the same page?

I'm using ASP.NET MVC 2 for the first time on a project at work and am feeling like a bit of a noob.
I have a page with a customer search control/partial view. The control is a textbox and a button. You enter a customer id into the textbox and hit search. The page then "refreshes" and shows the customer details on the same page. In other words, the customer details appear below the customer search control.
This is so that if the customer isn't the right one, the user can search again without hitting back in the browser. Or, perhaps they mistyped the customer id and need to try again.
I want the URL to look like this:
/Customer/Search/1
Obviously, this follows the default route in the project.
Now, if I type the URL above directly into my browser, it works fine. However, when I then use the search control on that page to search for say customer 2, the page refreshes with the correct customer details but the URL does not change! It stays as
/Customer/Search/1
When I want it to be
/Customer/Search/2
How can I get it to change to the correct URL?
I am only using the default route in Global.asax.
My Search method looks like this:
<AcceptVerbs(HttpVerbs.Get)> _
Function Search(ByVal id As String) As ActionResult
Dim customer As Customer = New CustomerRepository().GetById(id)
Return View("SearchResult", customer)
End Function
A good place to start might be NerdDinner if you havn't already.
In the mean time though The approach I'd use is to have a page that has my search box on it.
Then I'd have a <div> that I name "SearchResults". This will ultimately hold my results to the search.
I then have a PartialView which takes a model that has all the search results and renders them.
So when I click the button I do a call out to a jQuery action that takes the search parameter, performs the search and then returns my PartialView as rendered HTML.
Back in the client side I take that rendered HTML and replace the contents of my div with the HTML.
The keywords to google, or SO, are RenderPartial. This is back end code to render a partial view and give you html.
Also jQuery postbacks so that you can call an action in your controller.
use RedirectToRoute action result
link

Resources