Hello everyone,
I had a situation where I've more than one submit buttons in a form
which I need to send data to different controllers' action. From my
previous posts I got a suggestion to use JavaScript.
**Please Note: I'm not looking forward to detect which button is pressed
and redirect according to it.
Here is my code.
view
<%= form_tag "#", :id => "multi_cont_form" do %>
<div class="field">
<%= label_tag("From date") %>
<%= date_field_tag 'fdate', Date.today, :autofocus =>true, class:
'form-control' %>
</div>
<div class="actions">
<%= button_tag "Test JS", :onclick =>
"sendParams('receipt_rpts_create_path')" %>
</div>
<div class="actions">
<%= button_tag "Test JS", :onclick =>
"sendParams('payments_rpts_create_path')" %>
</div>
<% end %>
(I've only showed two buttons for example purpose.)
JavaScript
function sendParams(newAction)
{
var mem_code = document.getElementById("member_code").value;
var x = document.getElementById("multi_cont_form").action;
document.getElementById("multi_cont_form").setAttribute("action", "");
console.log("Checking form action received " + newAction + "..");
console.log("Current form action " + x + "..");
document.getElementById("multi_cont_form").setAttribute("action",
"http://www.localhost:3000/receipt_rpts_create_path");
var x = document.getElementById("multi_cont_form").action;
console.log("New action " + x + "..");
alert("New action of form is " + x);
document.getElementById("multi_cont_form").submit();
}
Even after I reset action attribute to "" and reassigned with new given
action, the final URL is being prefixed with current page's URL.
For eg:
If current page URL is "localhost:3000/reports/new"
The newly created form action using JS is
"localhost:3000/reports/receipt_rpts_create_path"
I can't prevent that "reports" from being prefixed.
And even "receipt_rpts_create_path" is not converted to
receipt_rpts/create URL.
Technically how to change "controller" option's value and "action"
option's value of <% form_tag %> helper using JavaScript. If its not
possible how to achieve it using JS.
--
Posted via http://www.ruby-forum.com/.
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4eeae1ad7eda65ec508deee146996651%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment