Determining controller based on post data in Rails - ruby-on-rails

In my Rails application I have a form with radio type inputs and post method. The user selects one of these radio options and submits the form. How could I set the routing such (in routes.rb or in some other way) that the controller would be selected based on the value of the selected input value which is now in the post data. In routes.rb the params hash isn't available, but could I access this data in some other way?

Two options come to my mind:
Use JavaScript to intercept the submit action. On submit, read the value of the selected radio option, change the target attribute of the form to the URL you expect to be used and submit the form.
To make the JavaScript unobtrusive, the URL of the target can be added as a data-target attribute to the radio options. In this way, once you get the selected value, you just replace the form target with the value of data-target. The benefit is that you can generate it server side and you don't have to hard-code the value.
Use an intermediate action. Always submit the form to an action that reads the payload and redirects to the relevant controller action based on the payload value.

Related

How do I pass the Value from a Dropdown Form through a Link in Rails?

I have a dropdown select on my page called language, which is in a form with other elements. When the form is submitted normally (which submits everything), I can access the value of the dropdown with params[:langauge].
I have another link/button on the page which, when clicked, should get the value of language and pass it to the controller. The controller could then run some ajax to change the language on the page. But how do I pass (just) the value of the dropdown through the link?
Update:
Should I get the value with Jquery, or is there a simpler way?
$('select[name="language"]').val()
If that is a link, the only way to use is query string
that link
You can get "href" of that link by JS, and then use this method to update query string.
Then the method will be able to process with correct params.
If that is a button, that would mean a mini form, you can set a hidden field inside the form and use jQuery to update its value. When sent, the controller method will know this value.
This javascript code should update the URL whenever the dropdown value is changed.
$(function() {
$('#link-button').attr("href", "/link_path/" +$(this).val())
$("select#language").change(function() {
$('#link-button').attr("href", "/link_path/" +$(this).val())
});
});

How can I filter the auto populate when editing a form in Rails?

In Rails, when you go to the edit action, it automatically pulls the information from the models and populates the form. If I had a CRUD that saves sensitive information, for example password or ssn, how can I filter the values so that it doesn't just show it in plaintext when editing the form?
I was going to change the value in the controller by setting it to ****, but the potential risk there is people may submit the form and it will update the SSN to ****.
I'm not referring to filtering the params so it doesn't show up in console (config.filter_parameters).
Changing the input field to type="password" should resolve your problem. Just don't set the fields in the controller if they are blank.
If you are using a form builder, change text_field to password_field.

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.

Ruby form submission

I am new to ruby on rails.
I have a form say form1.On clicking the submit button this form should take me to another form with the parameters passed.Please let me know how to do it and also how to access the form1 parameters in form2.
If you mean a multistep form, check out: http://railscasts.com/episodes/217-multistep-forms
The basic concept is that you will have to set the url to post to in your form tag (the default is a create or update action). Once in your new controller action, your previous form is available in the params hash. Without more details on your question, we can't provide better details.

In struts 2.0.14 the component <s:reset> button is not working after submitting the page?

I am using Struts2.0.14 for my application. I have a button to clear the textboxes. I have a few boxes which are stateful and values are persistent after the form is submitted.
My problem is that when I press the button before submitting the form it clears all values from the textboxes. But when I submit the form and press reset again, the textboxes do not reset.
I'm not sure if this is your problem, but: to "reset" the fields of an HTML form is not the same as to "clear" them, it just means to reset their values to their "default" values, those that were set (typically) in the value="..." attribute of the field tag.
Now, consider the typical server-side form validation workflow, when the user has entered some field values, submited the form, and get it back from the server with some errors marked (typically in Struts2, this corresponds to the INPUT result). Here, the new HTML page that is returned to the user will have its form fields filled with the previously submitted values. Now, from the client side perspective, those are the "default" values of the form: if you "reset" the form (perhaps after editing it), those are the values that will be restored.

Resources