Ruby on Rails:How to Implement Cancel Button in Form_Tag

Ruby on Rails : how to implement Cancel button in form_tag

If you want to clear/reset the form fields, do what weltschmerz suggests.

However, I'd generally expect a Cancel button to not clear the form, but to take me away from the form, meaning I don't plan on submitting it.

If you want the latter, I'd just make a link (or button) to the page you want to go to upon cancelling, such as :

=link_to 'Cancel', my_page_path

Or if you want a button:

= button_tag "Cancel", :type => 'button'

Then add this to your controller:

# before_filter for Rails 4.x and prior
before_action :check_for_cancel, :only => [:create, :update]

def check_for_cancel
if params[:commit] == "Cancel"
redirect_to my_page_path
end
end

How do I generate a cancel button in simple_form?

Should this be supported by simple form? I had a quick look at github and did not find anything related.

How about link_to "Cancel", :back ?

Submit Button in Rails partial on same line as Cancel button in parent view

You could pass the cancel URL as a partial local variable, like this (assuming your model is called Post):

# In new.html.erb

<h1>New Post</h1>
<%= render partial: "form", locals: {post: @post, cancel_url: posts_path} %>

# In edit.html.erb

<h1>Edit Post</h1>
<%= render partial: "form", locals: {post: @post, cancel_url: post_path(@post)} %>

# In _form.html.erb

<%= link_to 'Cancel', cancel_url %>

Hide submit button on form_tag in ruby on rails with js/jquery

Maybe not the jquery answer you are looking for (cause I'm not sure there is one), once you hide something via jquery and the page reload that jquery should re-run in order to hide the button again or you can not show the button in the first place using your erb view there are multiple options here:

if the record in the DB includes a user_id and you can check in your controller that this particular user already submitted the form fill a instance value in your controller e.g @already_submited_form = true (true if already submitted false if not) then in the erb view check if for the @already_submited_form if it was then don't show the button

<% form_tag matches_update_path method: :post%>
<% unless @already_submited_form %>
<% submit_tag “Make Matches?” %>
<% end %>
<% end %>

if you can't check for a record in the DB then maybe try using cookies in the rails controller: when the user submits their forms and the request hits the controller set a permanent cookie in the browser cookies.permanent[:already_submited_form] = true then reuse the logic from above @already_submited_form = cookies[:already_submited_form]

cookies in rails: https://api.rubyonrails.org/v5.2.1/classes/ActionDispatch/Cookies.html

if the request does not hit a rails controller you can also set a cookie in your js code

if you still want to use jquery to hide this maybe you can use one of these 2 methods to put a data field into the submit button which is generic (data-hide-on-page-load=true) then jquery the elements with that data and hide them (but it seems a bit unnecessary since you can do all this using erb which is one of the advantages of using ruby on rails)

Rails remote form submits when non-submit button clicked

You can prevent the default submission using event.preventDefault() on click event of the cancel button as:

$(".new-attribute-cancel-btn").click (evt) ->
evt.preventDefault()
$(this).closest(".new-attribute-section").hide()

Rails 4: Disable submit button after click

The problem was caused by invalid HTML code. See <div> breaks <form>
disable_with works now



Related Topics



Leave a reply



Submit