Update form based on dropdown selection - ruby-on-rails

I'm new to Rails and I'm trying to figure out how to change either the fields in the form, or the form itself, based on the selection of a drop down menu. I have thought about using AJAX, or about embedded Ruby within the form. Any direction would be great.

Well, obviously there will be javascripts involved at some point.
I think there are 2 solutions for you to choose from:
using ajax to fetch the new data to change your form upon dropdown changes
loading everything you need from the first time (when landing to the page) and then displayhing/hiding things only using javascript
Starting from this, I suppose your decision will be made depending if you want/can afford loading everything you need from the beginning... and if not you'll probably go with ajax.
Let us know if you need some more details. : )

Related

MVC Create: Submit without clearing form

It has been a while since I was in school for this, but looking for some help on a project I am working on in my spare time.
I have an MVC project in which I am currently working with a Create page. I would like to maintain the selections and inputs after submission. In general, I would like to be able to submit a form without anything refreshing so I can limit the selections for the next one.
This page is to add a security camera to the database. I may need to add several cameras and would like to limit the data entry. So keep the inputs after I hit the submit button and just make changes and then submit again.

Is it possible to have options on select change from when other select changes without javascript

There are a bunch of question on this but no answers on how to do it without javascript.
If you have one form that has 2 select boxes. The second select box has different options based on what you choose for the first select box. Here is a js example. Not all users have js enabled so for these users this option would be unavailable.
Can this be achieved solely using CSS3, HTML5 and Ruby? I would show what I've got so far in trying this but I got nothing.
What you are asking is how to manipulate the DOM after it has loaded without a client-side scripting language. This is not possible as far as I am aware; unfortunately that is not what you want to hear.
The proper solution in this case would be to have the user submit the page and generate the second selection box at that time. You will have to rely entirely on server-side logic to handle the problem. So basically something like:
Serve a page with just a single selection box
When the page is posted generate a similar page where the first selection is locked and display a second selection box with the possible options.
Continue the iteration until you have all of the required selections filled out by the user.
Serve the result that the user requested.

Rails - manipulating data in popup

I'm new in Rails, I have a Meal model which has many Products. Meals are assign to User (maybe this is important for a concept). In meals/new.html.erb I want to create new Meal as follow:
Click the button "Display Products"
On the same page (meals/new.html.erb) open modal (pop-up) with all products assigned to current user ( I have help method for current-user). It should be displayad like a list or grid with checboxes for example.
Then user can check few products and click "Confirm".
After that in meals/new.html should be appeared list of chosen products with additional input to fill their quantity.
So I have two problem here.
How should I display modal? Is needed any Ajax (I'm not so familiar with this technology)
How can I pass products between view and modal?
Could you help me a little to achieve these goals?
Regarding your first problem, displaying the modal is fairly straight forward. Essentially you will create a div with the proper bootstrap classes to be hidden when the page is loaded, and then create a button that makes it visible. I would recommend either reading over the W3Schools entry on modals, or from the appropriate part of the bootstrap javascript documentation.
Regarding your second question, this depends on exactly what you mean. The modal is part of the view, so if you're only trying to put information that is currently on the modal back onto the "page" behind it, you can do so fairly simply with javascript (copying content out of one element into another, or updating states of inputs). If, on the other hand, you're trying to use the modal to retrieve information from the server (for instance if you wanted to show a list of possible options, and then display detailed information about the selected items from the database) that would require Ajax.
If you have any snippets of code that aren't functioning as you expect, feel free to add your View to the question. In cases like this, usually the best way for us to provide help is for you to take an early crack at this, post the relevant code, and then seek answers for the things that behave unexpectedly.
I hope that helps.

Rails ajax post from button that sends values from outside of the form in the request

I'm somewhat new to rails and am wrapping a GUI around a CLI tool to give some non-technical users some self-service. Part of the tool contains a page where the user selects a few values from some drop downs etc, and then clicks one of a few buttons on the screen to kick off their desired actions. The fields where they make their selections are outside of the forms that the buttons belong to. On button click, I want to call a javascript function that will grab the values the user selected, and post an ajax call back to the server to run the command corresponding to the button, and then when the request returns I want to insert the command output into a text area.
The only problem I'm having is with getting the values of the fields outside of the form generated by button to, and including them in the ajax request. Outside of rails this is pretty trivial, but I'm having a hard time getting this to work. Does anybody have any suggestions or examples?
Thanks
There's really no need for what I was trying to do... it ends up I can just use the form_tag helper and multiple submit_tag helpers to create the submit buttons with different values. See this post for details: How do I create multiple submit buttons for the same form in Rails?
Also, here is a great write up on the javascript portion (it uses coffee script, but I didn't have any trouble following it even though I'm using jquery): http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html

Update content when new values are added to database?

I know my question might be ovbious for some of you, but I'm not sure how to do it, and I can't find it on google, probably because I'm not using the right keywords.
I'm using the latest rails. On my view I have a table generated by rails. People are able to add content to that table by sending a rails form.
I want this table to be updated with newly updated values, without having to refresh the page.
For example, if user 1 sent the rails form and added a new value to the database, I want user 2 to see this update without having to refresh the page.
I would highly suggest watching the Polling for Changes - Railscast that covers exactly what you are looking for.
This essentially will cover how to use jQuery to poll the server to see if there are any changes, and apply them to the page without refreshing(ajax).

Resources