I'm having trouble adding a particular html attribute to a Rails form submit.
= form_for :model do |f|
...
= f.submit 'Submit', tabindex: '3'
The tabindex property isn't showing up in the form. I also tried a html hash to no avail.
It just produces this html markup:
<button type="submit" value="Submit">Submit</button>
EDIT: The only alternative way I can think of is to use jQuery.
$('button[type=submit]').attr('tabindex', '3');
But that seems overkill. It seems like there should be a way in Haml.
The result of f.submit 'Submit', tabindex: '3' should be:
<input type='submit' value='Submit' tabindex='3'></input>
However, you appear to be getting a button element, and the tabindex is not showing up at all - so something else is definitely going on here.
If you have a custom form builder and override the submit method, this could certainly be the result. If you need that custom form builder and still want your submit element to be a button you'll need to make sure you're allowing an options hash through the submit method. Not sure what your current method looks like, but you might update it to something like this:
def submit(value, options={})
options.reverse_merge!(
type: 'submit',
value: value
)
button_tag(value, options)
end
Related
I'm making an input form whose features don't fit so nicely into the general form_for template rails provides. I figured I would write my own HTML mimicking the html output of form_for, but embedded with other form_for elements.
For fields I could use the rails framework for I did. For the others I made hidden fields to store what was going to Rails, and regular input fields whose values I manipulated with JavaScript to put into the hidden fields.
This is one such field:
State:<br>
<input type="text" class = "state name_input administrative_area_level_1">
<div class="field">
<input type="hidden" name="address[state]" id="state">
</div>
When I send the value of the hidden field to the console, I get a good response:
state 37
Which means the state field holds the value 37. Perfect.
Except that when I try to make my model, the params come in empty:
"state"=>"",
I do have one field that works that isn't hidden:
Street:<br><input type="text" id="street" name="address[street]">
So I changed the state input type to number, which is what it would be if it weren't hidden, but same result. Why can't rails find my param?
You can show post more detail. I can't understand 100% things you do because you write pretty simple. But i guess that you need test params name is address[state] but it's not only state becase if only state of course it nil.
if html of you rendered same up then i suggest you should put one debug in controller to see params return controller when submit form.
For example: in controller you add line:
logger.debug params
to see right params that form send.
If your form is not tied directly to a object model, it might be difficult to use the form_for helper. In those cases you should use form_tag helper. Read more about form_tag helper here.
Refer this stackoverflow answer for the difference between form_for and form_tag helpers.
Hope this helps.
Here's my examples that produce the same result :
# `enterprise_registration` is an already created/saved object
form_for enterprise_registration, method: :put do |format|; format; end
form_for enterprise_registration, url: logo_url, method: :put do |format|; format; end
form_for enterprise_registration, url: logo_url, html: {method: :put} do |format|; format; end
This returns the form with the method attribute set to POST.
Why is that happening, do you think? And how can I make it a :put request?
Update
I now understand that Rails forms embed a hidden _method and set it to put, but my form is still getting delivered as a POST that is preventing my form from finding my matching PUT URL
form tag has only GET or POST method allowed. See also here for more explanation. Rails, however, has his own method to handle GET/POST/PUT/PATCH requests. If you would examine any of your form defined as either form_for or form_tag in Rails, you will notice that first element of the form is a hidden <div> which contain two hidden fields:
<div style="display:none">
<input name="utf8" type="hidden" value="✓"><input name="_method" type="hidden" value="patch">
<input name="authenticity_token" type="hidden" value="9i5eRhwhx4NhvSxqIJm6cv9x6NSlY82hpNpfrpk/I0c=">
</div>
First field called _method contains the form action which is the request type for controller.
Web browsers are actually only programmed to receive POST and GET requests (I'm not sure why). The way Rails mimics full REST (which includes put and delete) is include those in hidden fields. So technically it's sending a POST, but with the PUT attached sort of awkwardly in that hidden field.
I'm assuming that Rails is still doing that sort of conversion behind the scenes still.
I made a rails application, and I used datagrid gem to handle filters, pagination,and orders(ascending and descending). I was supposed to write <%= f.datagrid_filter filter%> to filter according to a filed of the table(Ex:title field in topics table).
Now <%= f.datagrid_filter filter%> returns a traditional html input tag like below. <input id="topic_report_title" class="title string_filter" type="text" size="30" name="topic_report[title]"> in the html console.
Now I want to put placeholder in that helper method only.
Can anybody help please?
Have you tried to do the following: <%= f.datagrid_filter filter, :placeholder => "placeholder text"%>
Reinstalling a Rails app on a new server. Part of the app can fork in one of two directions based on the button the user selects. This part isn't working, and when I look at the log I see the values that I gave the form, execept for the commit portion of the params hash. This seems to be why the app isn't working as expected (since there's nothing in params[:commit], but I have no idea why commit would not be passed in; the request is definitely a POST request, and all of the other parameters are there.
Had a simular problem with a disable-button-on-submit feature. We solved it by adding a hidden input field with the same name and value before submitting the form.
function disableButtonAndSubmit()
{
var input = $("<input type='hidden' />").attr("name", $(this)[0].name).attr("value", $(this)[0].value);
$(this).closest('form').append(input);
$(this).attr('disabled', 'disabled').html('Loading…');
$(this).closest('form').submit();
}
$('#somewhere button').click(disableButtonAndSubmit);
Just add name: "commit", value: "Save"to your form submit button:
form_for #object do |f|
...
f.button :submit, "Save", name: "commit", value:"Save"
end
and then you will have params[:commit] equals to "Save" in the controller.
I ran into this same problem, and the answers here pointed me in the right direction. However, rather than the suggestions to be adding hidden form inputs or giving up on the double submit block, you can simply add a setTimeout function on your double submit block with a timeout of 1 millisecond, which allows the double submit block to work without preventing the submission of the button.
Check that your submit input is named commit or it's label will not be sent.
The resulting html should be:
<input type="submit" name="commit" label="...>
I looked into something like this awhile ago, where there is inconsistency in how different browsers would pass in the value of a submit button on a form. I found the only practical solution was to have javascript in the button to set a hidden field, and use that value instead.
Here is some of my code to differentiate between a save and exit, which goes one way, and save and continue which go another:
<%= hidden_field_tag 'step_commit', '' %>
<span style="float:left;">
<%=submit_tag 'Cancel', :name=>'cancel', :onclick=>"javascript:location.href='/';return false;" %>
<%=submit_tag 'Save and Exit', :name=>'exit', :onclick=>"javascript:$('step_commit').value='exit';" %>
</span>
<span style="float:right;">
<%=submit_tag 'Save and Continue', :name=>'continue', :onclick=>"javascript:$('step_commit').value='continue';" %>
</span>
I am trying to write a rails application which lets you go to a certain page, say /person/:id. On this page it shows a set of available resources. I want each resource to have a button next to it, which reserves that resource to that person (by creating a new instance of an Allocation model.) As an extension, I'd like several buttons by each resource, that cancel reservations and do other things. I'd also like to input data alongside some of the buttons, e.g. to allocate some % of a resource.
My problem is I can't work out how to sensibly do this without repeating myself, or having a very hacky controller. How can I do this without matching on the value part of the submit buttons (the text on the buttons), or using any javascript?
Additionally, if you have two forms on a page, how do you set it up so changes on both forms are saved when any submit button is clicked?
im using jQuery, and this is what i did :
<script type="text/javascript">
$(document).ready(function() {
$('#bulk_print').click(function(){
var target = '<%= bulk_print_prepaid_vouchers_path(:format => :pdf) %>';
$('#prepaidvoucher_bulk_print').attr('action', target);
$('#prepaidvoucher_bulk_print').submit();
});
$('#bulk_destroy').click(function(){
var target = '<%= bulk_destroy_prepaid_vouchers_path %>';
$('#prepaidvoucher_bulk_print').attr('action', target);
$('#prepaidvoucher_bulk_print').submit();
});
});
</script>
<% form_tag '#', :method => :post, :id => 'prepaidvoucher_bulk_print' do %>
your form details
<button class="button" type="submit" id="bulk_print">
<%= image_tag("web-app-theme/printer.png", :alt => "Print Selected Vouchers") %> Print Selected Vouchers
</button>
<button class="button" type="submit" id="bulk_destroy">
<%= image_tag("web-app-theme/cross.png", :alt => "Delete Selected Vouchers") %> Delete Selected Vouchers
</button>
<% end %>
The idea is to change the form action on the fly, based on which button is clicked
Make each row in the list a form and put the info about the item in question there. Of course, you'll need to submit and reload the page with each action. The only way around this is to use checkboxes instead of buttons and make it one big form — or to use Javascript.
As for your second question, if you want to have a submit button affect two "forms," you should make them both part of the same form. You can have multiple submit buttons on the form if you need to. Otherwise, you could dynamically generate a third form with Javascript filled with the values from the original form — but that wouldn't work in all cases (e.g., file inputs).