Ruby on Rails initial controller setup - ruby-on-rails

I'm getting started with Ruby on Rails and have some questions. I currently have a website that is written in Perl/HTML/Javascript, etc. My goal is to convert this website to use Rails. I'm running into a stubmling block on how to get started.
Here is the general overview of the current website:
The main page has 3 selection lists that get populated on page load (via SQL). The user can select an option from one of these lists (say a date as one of the lists stores dates). Currently, using AJAX, this calls a Perl CGI script which queries the database with the chosen date and returns a table containing the appropriate records (fills in division next to the selection lists).
There are no other pages on the website (besides a changelog) as everything happens on the main page. The selection lists do not get refreshed after each selection due to AJAX.
My question is, would I have a controller called Selection to populate the lists and another called Output to create the table of records? Does that setup makes sense?
Thank you!

This is my understanding about your requirement.
1) You have a page where there are three selection boxes
2) If a user changes any one of the selection box, then you are posting data to backend to get output based on the selection (using Perl CGI script)
The output is loaded after you get response from backend.
If my understanding is right, below are my suggestion.
Suggestion :
You need not to have different controllers for each action, in Rails you may have multiple action within a single controller.
Say you can name your controller and action as below
Selection Controller (Can have multiple actions under it as below, you can define custom names for each action in controller and add routing information in app/config/routes.rb file)
- GenerateList
- CustomAction1
- CustomAction etc.,
EDIT :
You should not use single controller for all your actions in the website, you need to categorize
functionality and create multiple controller/actions accordingly.
Let me know if you face any difficulty in creating and running custom actions.

Related

Application Wide Filter/Scope via Form in Rails

How can I implement and application wide scope/filter on a certain resource in Rails?
Example:
My application can collect questions asked by certain people/teams for certain categories/sub-categories. The application has multiple pages with statistics on these questions (e.g. word analysis, questions by subcategory).
Now it would be nice to implement a "scope-like" condition on said statistics. Let's say I want the stats to only be for the questions asked within the last seven days. Then I switch to the next page with different stats and the scope stays the same.
Currently I am using the Ransack Gem for filtering. I would just add the filtering form on the main view (layout) so that one can modify the parameters. But how would the logic work in the backend? So that the filter params are passed back and forth.
Store the filtering params in the session like: sesssion[:filtering_params] = { whatever: they, are: here } and then they'll be available on subsequent requests.

Proper way to remember multiple parameters across requests in Rails

My application feature a "main" page where most of the action happens: There are tags for filtering and a list of results in a (paginated) table, plus the possibility to select some or all results in a "shopping cart".
This page has to keep track of a whole lot of things: what tags are selected, what items are selected, and how the result table is sorted and what page it's on. Everything has to persist, so if I select a new tag, the page must partially reload but remember everything (sorting, what's selected).
Right now I'm handling everything with parameters, and for each action taken on the page, all links (select a tag/item, change page, sort table) are updated to include previous parameters + the relevant new addition. This works, obviously, but it feels kind of inefficient, as I have to reload more of the page than I want to. How is this situation normally handled? I can't find that much info on google at all, but it doesn't feel like a particularly uncommon case.
tl;dr: How to best make sure all links (to the same page) always include everything previously selected + the new action. There are a lot of links (one per tag to select/deselect, one per result item to select/deselect, one per sort option, one per page)
There are five ways to do that:
Method 1: By parameters
You mentioned this. I never think of this as it's too troublesome. Anyway it's still a solution for very simple case.
Method 2: By cookie
Save the settings to a cookie and read the cookie in controller to arrange layout settings.
Method 3: By LocalStorage
Similar to cookie but allows more space.
Method 4: By Session
If you are using ActiveRecord to save session, this could be the best solution for pure pages loading. Save the user preferences into session and load it in next layout.
Method 5: Use Ajax
This is the best solution IMO. Instead of whole page loading, use Ajax to refresh/retrieve changes you need. Using together with above method, a user can even continue his last preferences. This is the most powerful and should be applicable to your case which looks like a web app than a website.
Have you tried creating model for all those attributes? and just always load the 'latest' when on the page load, if you dont need them you can always have a flag for that session.

Update attributes of embedded new record in Rails & Mongoid

I'm trying to write a piece of functionality that creates a record from a template in my Rails app, but with some customisations from the user.
The difficulty comes when trying to let the user override a field in an embedded document: the document is duplicated rather than updated.
I start with an object of type, let's say, ExternalLinkGroupTemplate, and it contains a number of ExternalLinks. It's used to create an ExternalLinkGroup which also contains ExternalLinks. The user should be able to edit the link text when it's cloned but not the URL, so the user is presented with a form When it's copied the user should be able to I can create a copy of it with (simplified because some of the code is in different files, remove tests for success etc):
#link_group = ExternalLinkGroup.new
#template.links.each do {|link| #new_link_group.links.push link.dup }
#link_group.update_attributes(params[:link_group])
When the link group is created, it contains twice as many links as the template, and only the second set has the changes. Clearly, Mongoid/ActiveWhatever is creating new items rather than updating existing ones.
I don't want to just pass through all the fields in the parameters without duplicating anything because there's actually some inheritance going on and some of the links will be of different types and have extra fields, and I don't want to code a partial for each of them that just has a bunch of hidden fields. Also, I don't want the user to be able to modify the hidden fields and submit whatever.
I can't reference them by _id because they're not saved yet. I was expecting the array index of template[links_attributes] to be sufficient but it's not.
What do?
Using Rails 3.2.13, MongoDB 2.2.x, Mongoid 3.0.5.

How to handle front-end content in order to display menu items based on the current user?

I am using Ruby on Rails 3.2.2 and I would like to "handle" / "generate" the proper front-end content so to follow a "common" way of managing menu items mostly based on the current user "equality". That is, I would like to display different menu items based on
the current browsed page;
the current user that is accessing that page (the page could be related to a user that is not the current user).
Where (for example, in controller, view or model files) and how (for example, stating checks related to the current user in controller, view or model files) I should "state" / "put" those "conditional statements"?
At this time I think (mostly for matters relating to the current browsed page) I could handle this matter directly in controllers by using helper methods...
I tend to put this into the View files with simple or static cases coded into the View file and more complex cases which would require more than just one line of coding into the helpers.
But in order not to bloat the view file, I suggest to use partials to offload blocks of code from the view file.
One of the reasons why I use the views is that I will do certain user based formatting anyways, e.g. if I list all the orders, then the normal user will see certain columns displayed, whereas if the admin logs in, he will see a couple of additional columns. This is typically done with something like
<%if #curreny_user_type == 'admin'%><td> ...... </td><%end%>
Thus before I now start splitting up some of the user based logic into the controller and others into the views, I'ld rather have them always at the same place.

Stack Overflow-like tagging behavior in Rails

I have a new application that contains a Modela called "Campaigns". Each campaign is able to have any number of tags associated with it.
What I am attempting to do is impliment a Stack Overflow-like behavior with these tags. Namely, that when you create a new campaign, it gives you a text-field that will auto-complete with tags that already exist, and start anew every time you put a space. Additionally, should the tag not exist, it should create a new tag.
This railscast is a step in the right direction, but it only allows for one "tag" at a time.
Can anyone point me in the right direction?
In the model layer I'd go for a plugin like: https://github.com/mbleigh/acts-as-taggable-on.
In the view you want an autocomplete plugin (personally I use this http://docs.jquery.com/Plugins/autocomplete). Then either generate a controller for the tags and fetch the autocomplete list remotely or just = them in the page.

Resources