For one form, I want to have two submit buttons , named:
Appove, Decline
when the user clicks on Approve it should send approve: true as well
when the user clicks on Decline it should send approve: false as well
Basically the buttons should work like a checkbox
Is this possible with Rails and how to implement it? Thanks
If you're using the standard submit form helper, you will get returned a param with the key "commit". You can test for this in your controller code.
<%= f.submit 'Approve' %>
<%= f.submit 'Decline' %>
in the controller...
def create
approved = params[:commit] == 'Approve'
The approved variable will then contain true or false and you can use it as needed in the rest of the action / method.
You can do it but you need js/jquery for this. You will have hidden checkbox that you will check in proper way and two buttons.
Lets assume that your form has id 'form'. And you checkbox has id 'approve_checkbox'
In this case you need submit function for something like this.
$('#approve_button').on('click', function(e){
e.preventDefault();
$('#approve_checkbox').prop('checked', true);
$('#form').submit();
});
$('#decline_button').on('click', function(e){
e.preventDefault();
$('#approve_checkbox').prop('checked', false);
$('#form').submit();
});
Of course you can simplify this code, but I think idea is clear.
Related
I have a page where a deactivated user account can be activated - but before they can be activated, they need to be assigned a new password. Currently I have two buttons to perform these actions:
At the end of the 'add password' form there is this button:
<%= f.submit %>
A little further on the page there is this button, which sets the status of the user to 'active':
<%= standard_button 'Activate user', update_status_user_path(#user),
:method => :patch %>
If possible, I'd like add the functionality of the second button to the submit button.
I don't fully grasp the magic behind forms and their buttons, so I am wondering whether this is possible and if so, how I could accomplish this?
You can think of a button as a mini form. View the docs to get more information about the button_to rails method.
In your form_for, there will be a url which specifies where the form is posting to. In that controller method, you can include the logic to update the user's status to 'active'. Something like
def some_method
user = User.find(params[:user_id])
user.update_attribute(:status, "active")
end
I need to submit my form every time user change the fields in this form.
How can I do this using ajax?
Add below code in your javascript:
$('#text_field_id').on('blur', function() {
$('#form_id').submit();
});
Here, when you lose focus from your text field, your desired form(with id #form_id) will be automatically submitted.
In the end of "create" action, I rendered a view that changes attributes of the form, like this:
<% if (#register.id) %>
$("form").attr("action", "/posts/<%=#registro.id%>");
$("form").attr("method", "put");
<% end %>
Doing so, the form now acts like an "edit" form, not a "create" anymore.
My script looks like this:
$("form").keypress(function(){
doSave();
});
function doSave() {
$("form").submit();
}
I want to change to state of the submit button when a user clicks on that I want to make these transitions on that button.
Text change to "submitting..." and state of that submit button becomes "disabled"
I know how to do this with Jquery something like:
Let's say that the button has a #derp id.
$("#derp").click(function(){
this.toggleClass("pressed");
});
Basicly this will add or remove the "pressed" class from the item based on its state. Just add the css for .pressed and you're ready to go.
#derp {...}
#derp:hover {...}
#derp:active {...}
#derp.pressed {...}
Or we do using different selectors.
Question: Is there is default functionality in ruby on rails.
Update:
I am using ajax based form submission and when i try to click 2-3 times form submittion quickly. Its actually submitted 2-3 times. I want to prevent that thing and I am using simple_form
<%= f.button :submit, "Submit Review", :disable_with => "Processing..." %>
You can use :disable_with option:
http://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#method-i-button_tag
To make :disable_with Work with Ajax Remote You have to edit your public/javascripts/rails.js and change
document.on("ajax:after", "form", function(event, element) {
to
document.on("ajax:complete", "form", function(event, element) {
I have one select_tag and multiple button_to buttons on the same page. I'm looking to use the parameters from the select_tag for several of the buttons, is there a non-form way to do this?
The reason I don't want to use a form is two fold:
1. the number of buttons is dynamic
2. the positioning of the buttons do not follow the form structure (object at the top and submit at the bottom)
-here is one of the create methods that are linked to one of the buttons
def create
params[:options].each do |x|
#connector = Connector.find_or_create_by_options_id_and_follow_id(x.id, current_user.follow(#product).id)
#connector.save
end
end
I checked and this params[:options] is always nil no matter what is selected by me when I test
<%= select_tag :options, options_for_select(#current_user_options.map {|p| [p.name, p.id] }), {:multiple => true} %>
button_to method actually creates a form which has a submit button, with the form action being the url which you had mentioned in button_to. So when you hit the submit button, obviously the options param will be nil, since the button_to form has no data in it
You can use a form itself, and form does not mandate, having an object at the top and submit button below. Submit button just submits the form irrespective of its position in the form
It sounds like you actually need a second field in your form that's a select field or radio buttons to choose your action. Then you have one button that submits the form, and in your controller you can then parse that new action field and decide where to redirect the user.
I have a form where I create a model and I want a dialoge box to appear if the user navigates away from the page, unless they click the save/create button.
I have this javascript code that works anytime the user leaves the page; meaning this dialoge still appears when the user clicks save/create.
#javascripts/workouts.js.coffee
window.confirmExit = () ->
"Your changes will not be saved.";
#workouts/new.html.haml
= render 'form'
:javascript
window.onbeforeunload = confirmExit
#workouts_form.html.haml
= simple_form_for(#workout) do |f|
# some input fields
= f.button :submit
Now I know that I only want the confirm exit called if the submit button is not clicked, but I am not sure how to go about doing this.
What I usually do is I only show the confirmation when changes actually have been made to the form. To do that, you'll need to scan the forms on your page, serialize them and store it in memory. Then, when the user leaves the page, serialize the form again and see if there is a difference.
Otherwise, you might want to bind some function to submit event of the form, setting some global boolean like window.formSubmitted to true. Check that variable when leaving the page to determine if you want to show the confirmation box.
So I figured it out, at least it works for chrome and safari although right now I am under the impression that it will not work for ie. What I did was create a boolean that is set to false and only set to true when the submit button is selected.
#javascripts/workouts.js.coffee
window.submitButtonClicked = false
window.confirmExit = () ->
if window.submitButtonClicked
null
else
"Your changes will not be saved."
$(document).ready ->
$('#target').submit ->
window.submitButtonClicked = true
#workouts/new.html.haml
:javascript
window.onbeforeunload = confirmExit
#workouts_form.html.haml
#target
= simple_form_for(#workout) do |f|
# some input fields
= f.button :submit
I hope this helps anyone who was curious. Note that the #target line is actual haml and not a comment.