rails best way to handle optional parameter - ruby-on-rails

I want to display a list of stocks when the user logs in (default parameters). This smells like an index action. However, I will also make a form for the user to select the market capitalization of the stocks (that is to refine the universe). The submission of the form will send parameters to some action (I am not sure if I should use the same index action) and then do an ajax update of the list. This ajax doesn't do anything to the database, it just updates the parameters for the database query.
The first time the user visits the site, they will see the default parameters for the query, but I also want them to be able to change the parameters later and renew the list according to their parameter through ajax.

Yep, a list of stocks sure sounds like an index action. Do you mean that you want to filter the list depending on some form input?
You can make the form send to the index action with a get method, and in your controller, just read the params, and query your #stocks however you want your filter to work.
This is the non-Ajax solution, so users without Javascript will be happy. When you get this working, you can easily AJAXify it.

Related

Rails form changing number of fields

In rails, I have a UserOffer model, that has_many Steps.
When I create a new UserOffer instance, I want that form to include a dropdown that says “select number of steps of that UserOffer”. Based on the number of steps selected (n), I want the form to expand, and include n extra fields, called “step 1 title”, “step 2 title” … “step n title”.
I figured I need to use nested attributed to do this, but I wanted to know how the form would look like, preferably without using JS or Ajax (just with RoR).
I would appreciate any help
If you want to do that, at the same form without refreshing the page, you will need to use an ajax request to the server, in order to add the steps to the UserOffer model. Remember that RoR only let you do things on the render step of the DOM, in other words, if you had rendered the form you will not be able to do another changes on the page without use javascript.

Query Parameters in URL from a Rails form

I have a filter sidebar that starts with a Rails form_tag and contains a range slider and a bunch of check_box_tag.
It posts and filters fine. It even persists on the next page as I render it with the checkbox values.
However, if you refresh the page, or send a link to someone, the filters are lost.
The only way I've seen how to do it is to use redirect_to and merge the params, but I'd rather not make a second call.
How can I pass all the options as query params?
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.

Rails - Reload action with new value in params

I have the following relation in my model:
A user can have several consumers
According to that, I call several actions from different controllers that depend on that consumer_id in the URL. So, I do stuff like:
/:consumer_id/products/all
/:consumer_id/locals/all?params...
I want to be able to given that I am in a particular view, let's say /3/products/all, be able to refresh, redirect or whatever is the best to /4/products/all, with a form that shows the different consumers attached to that user.
I know how to display the form, but I fail to see which action should I put given that I want to load the current controller, current action and the current params, except that I want to change the params[:consumer_id] and the session[:consumer_id] to the one chosen by the user in the form of the view.
What's the best or appropriate way of doing so?
This is a link to the same page with the same parameters and one additional parameter:
<%= link_to "Show more results", url_for(params.merge(:per => 100)) %>
If you want to modify a parameter rather than add a new one, you can modify the params hash just as you would modify any hash, and use url_for again.
Inside the view you can access both the current controller using controller_name and the current action using action_name
Your question is not clear. You want to choose consumer_id from your form, then use that consumer id inside the same page or for links to other pages?
If you want to use the consumer id for the current page, you need to use javascript or JQuery to update all attributes of the current page.
If you want to use that consumer id for links,
you may also use JQuery to update all links on your page
Or, you can submit the form to the server with new consumer_id.

How do you handle parameters for sorting, paging and filtering?

This is the usual thing: you have a list of items with several attributes. You can :
sort the list according to each of the attributes, in both ascending and descending order
filter (search) the items, again on all attributes
navigate between different pages of results
All of this gives you differents parameters for a given page:
sorting attribute and sorting order
pairs of attribute name and values for the filtering
page number
How do you handle the propagation of all these paramters between your pages ? Let's say you can edit one item, and when you go back, you'd like to get on the same page you where.
Do you simply put all parameters in the url (and pass them as "return parameters" to the edit page) ? Do you put some in the session (maybe sort and filter parameters) ?
I like to make them part of the URL so that if someone bookmarks the page or emails a link to the page, it will render the page exactly the same way. You can't do that if you depend on session state.

How can I re-populate a list in a <div> after adding an item to it using AJAX?

Specifically, I have a number of pages in my Rails app that use the same partial. In the action handler for each page I create an array object (e.g. #list_elements) based on a database query. Each page uses a different query so that each page has different list elements in it. At the top of each page I have a form_remote_tag containing an edit field, allowing the user to add a new element in a dynamic, AJAXy fashion (think something like Twitter 'What's happening' box).
My problem is that when the AJAX command fires I need to reload the list to include the newly added item, but the contents of the list were determined by a database query. I need to remember which query applies to the current page (i.e. controller action) so that I can run it again. I thought about storing something in the rails session structure but it seems like overkill - it's like storing the current page all the time.
Anybody done anything like this and have a nice Railsy way to achieve it?
Ben
Couldn't you just re-render the partial in your rjs template?
page[:div_element].replace_html :partial => 'partial'
If you perform the query and define the array in the controller action, then an ajax call will refresh that array.

Resources