Rails ajax post from button that sends values from outside of the form in the request - ruby-on-rails

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

Related

Update form based on dropdown selection

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. : )

Rails - Check Boxes Calling Methods

I'm making a 'check_list' for my company. Each row of the table will be ONE event with 10+ check boxes that are labeled with the table header. Each time a check box is clicked I want to save that user's ID and Date. Is there a way to call a server side method to save this without submitting a form?
I just don't want to use a submit button because I need as much room as possible for check boxes.
I welcome any ideas or tips/tricks.
I am using Ruby on Rails.
Use jQuery, attach to the change event, and use ajax to call the method.

How to build Preview functionality before form submit in Rails ?

I have a simple scaffold and I want the user to see (preview) the form data before he submits it. It looks to be fairly straight forward problem but surprisingly I am not able to figure out how to do it. I found this but it looks to be somewhat dated (2010 question)
Is there any gem or jquery plugin which can simplify this preview functionality ? any suggestions on how to do it in rails 3.2.x ?
Thanks
It really depends on what kind of "preview" you want to include - either "preview page" or "rendered fragment":
Preview page is what some services are showing: summary of all inputs shown in form of separate page with button "confirm". It doesn't require javascript or anything fancy - just click "next" on form page, see data typed just a second ago and click "confirm" or "back to edit". If this is what you require then you can do it simply by creating new controller action(preview?) which would initialize model with passed parameters(just like "create" action would, but without saving) and generate template with "preview" shown and hidden form that will be passed to "create" action after clicking "confirm". Alternatively you can simply modify "new" form so it would hide form depending on called action and show preview instead.
Second option is "render fragment" - it will live-update part of your current page via javascript. Depending on how complicated this view will be and what kind of operations you will need to do before showing template it might be good to use some javascript plugin or send request to server and obtain rendered partial. In former case you can select from very easy implementations(like this) up to complete solutions dedicated to it. On the other hand if you choose to render it on server then simple ajax request with all params of form should be enough to provide you with HTML output that can be put directly into DOM element.
I don't know about any gem which can do that.
I think showing a modal with jquery and getting data from data-elements is a simple way to go.

Make checkbox as a link?

I want the html.checkbox() in ASP.NET MVC to be as a link that goes to an action controller (GET and not POST).
Question:
It is possible to make the html.checkbox() to act as a "actionlink" instead of going to the FormCollection without using Javascript and JQuery or any additional plugin?
This wouldn't be possible in any framework as checkboxes are just static input elements. They have no ability to trigger any sort of action on their own, hence where javascript and event handlers comes in.
You could try making a form who's method is GET, but it would still have to go somewhere.. I guess you could make an action that handles various states of a checkbox and then routes to the correct page, however you'd still need to submit this form by having the user click a submit (again because the only way to trigger an action from clicking a checkbox is by using javascript)
You could also fake a checkbox using css and checkbox-like images.
text goes here
If you need to show it as either checked or unchecked, you would use two classes with two different checkbox-like images.
I doubt this is possible without javascript.
Except for buttons and links, no controls actually do anything but change their own value in the DOM.
It would however be rather trivial to add an onclick to your checkbox that would do whatever (including a GET request).

Parameters through postback

I'm working with Ruby on rails 2.3.4 and I'd like to pass some parameters from one page to another ones the first one is submitted.
For example:
On the first page, I have a form that when it's filled a Preview button can be clicked to check all the info entered before submitting the form. That button redirects to another page with the info entered before, but I don't know how to get it in the second page.
There are two possible solutions:
You can emulate the stepped form filling by creating a record in first form and saving it with status "unverified" or "pending". This way you won't have to deal with hidden form fields in 2nd and 3rd pages. All you'll need to pass is the id of pending record. You'll just need to update record status to "active" once the data is confirmed.
Use client side paginated from (all popular JS frameworks have plugins for this). Hence you will only display different <div>s in single loaded page (something like an interface for a setup wizard).

Resources