Access data in a rails form for a separate input - ruby-on-rails

I'm developing a form using simple_form_for. I want my second input to be a collection derived from the first, but I can't figure out how to access the first input without submitting the form.
Is there some way to access a previously inputted value?

I’m not aware of how to do this within RoR natively; however, this behaviour is achievable using a JavaScript tool such as AlpineJS.dev, More specifically AlpineJS x-model to link JS data to the form input.

Related

Is there a way to automatic load control values to Orbeon Forms?

We've just recently bought and started using Orbeon.
When the user edits a form, and want to save it, he press the save button, and we defined it to send the Orbeon-style XML with all the controls values to my C# REST API. When the user wants to continue editing the form, I call the Orbeon form, and there is a trigger, what fires when page loads, and it starts a REST API call to my C# REST API, to get the control values. Now we have to define map EVERY value. Eg. ControlSurname -> ControlSurname, ControlLastname -> ControlLastname ... etc.
Is there a way, that my C# API gives back the same Orbeon-style XML (maybe some values changed due to the business logic, but not the structure), and Orbeon parses it and put all the control values to its place? (Without mapping all the 87 control?) (It works in the save methon, you dont give a mapping when save...)
You can configure Orbeon Forms to load the initial data from a service. See the section Initial data from service for more on this.
The benefit of this approach is that you won't have to do any the mapping work you were referring to, and the drawback is that you have to make sure that the structure returned by your service corresponds to what the form expects, or your form might end up with missing fields or other problems.

Form submit with the form context in HTMLUnit

I have a project where I am converting some old HTTPUnit code to HTMLUnit. Among one of the issues I have ran into is the form.submit() option of HTTPUnit.The form.submit works without having to specifically finding the submit button for that particular form provided I have selected the correct form while initializing the form variable earlier.
Is there something similar in HTMLUnit that I could use?
Just to explain the significance of what I am asking, I am testing a shopping site with several forms (for different product) and each form has it own form button with no specific attribute to separate them. Every form has its own different structure and hence finding the submit button using xpath is a bit difficult.
Specifically what I am asking is there a way in HTMLUnit where I could submit a form on basis of the form itself and not the specific submit button? And if not given the above circumstances could anybody please suggest a workaround?
Thanks in advance.

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

How does Rails persist text in input field?

I have a regular text input field, whose value is submitted via AJAX. After I migrate away from this page, and hit Back button, I can still see the value of the field there.
My questions are:
Does most of modern browsers support this user-entered values?
How can I remove the persisted value using Rails?
Thank you.
Your browser is remembering the form input values and displaying them when you go back. This likely doesn't have anything to do with Rails*.
You can't really do anything via Ruby/Rails code to change this behavior, but you can do it with JavaScript. Here's a random SO question that covers it: Reset form on Back button.
I'm not sure if there's a perfect way to handle this in every browser. Also note that many users would prefer that the browser keep the form values they've entered.
Note: I only qualify with "likely" because it is possible to write an app that explicitly remembers form values and restores them when the page is visited again, but by default, Rails isn't going to do that. It'd involve a fair bit of Rails and JavaScript code.
Does most of modern browsers support this user-entered values?
Yes, I believe they all support it to various degrees
How can I remove the persisted value using Rails?
This is a client side issue, not a Rails issue. You could use javascript to clear the input fields
A similar question and answer: Clear all fields in a form upon going back with browser back button
BFCache? https://developer.mozilla.org/en-US/docs/Working_with_BFCache

Flash and ASP.NET MVC to post data to server

what design approach would you take for security and scalability (and perhaps level of effort) when posting an ASP.NET MVC HTML form that also has a Flash component, as described in the scenario below?
I have a scenario where there's an ASP.NET MVC site, and a page that requires user authentication & authorization to access (using ASP.NET forms authentication). On that page there's an HTML form. The form also has a Flash component. The form has a few text fields, and the Flash component has binary data that needs to get submitted to the server in tandem with the HTML form fields. When the user hits the submit button on the HTML form, the form contents and binary data from the Flash need to get submitted as part of one atomic unit, so to speak.
I know that I can use HTML / Flash JavaScript bridging to post the form either through JavaScript, or through the Flash component. I could even do both, perhaps posting the binary data from the Flash component when the user clicks submit, and then posting the HTML form content following that.
From your experience, what approach would be the path of least resistance to post the form with? Considering the user authentication and authorization part, I imagine that Flash would higher effort than HTML. What about the user authentication aspect? If the page posted from Flash to the server, would Flash also have to authenticate the user, in addition to the standard HTML authentication form?
My Flash binary data should not typically be greater than 300KB, often less...any opinions / insights are greatly appreciated!
EDIT:
I also vaguely remember that with Flash 10.1, ExternalInterface seemed to not work right when transferring binary data to JavaScript, am I mistaken with that, and that you can easily transfer 300KB or more of binary data from Flash to a JavaScript variable that is then posted to the server?
Thanks!
That's one way of doing it:
Make your flash component call a javascript function through ExternalInterface.call().
Make the called javascript function change a hidden field inside the form.
When the form is posted, the hidden field will send the desired value to the server.
I guess this is also the safest way - since the Flash component will not communicate with the server. Everything you need is a standard, non-AJAX form post.
Edit
Sorry, I guess I should have paid more attention... If there is too much data being sent from the flash component, maybe you should post it directly to the server.
Still, you could use ExternalInterface to synchronize the whole process. Make Flash call a server-side method (I would use FluorineFX for that, but your opinion may vary). Then .NET will return an ID, meaning it has received and saved the binary data for future use. Then call ExternalInterface to set that ID to a hidden field. After that, when the HTML is posted to the server, the server-side action method just need to retrieve the binary data using the posted ID...
The problem here is that you will end up with some binary data that will never be associated to any form post... But that's OK I guess, just run some "garbage-collector" script from time to time.
About the authentication issue: FluorineFX does implement .NET authentication, and it is able to retrieve the current logged in user. Of course, there are some issues.

Resources