Last active
January 18, 2023 22:35
-
-
Save scarfacedeb/03ba3bf5f60b1c043d12 to your computer and use it in GitHub Desktop.
Form with multiple button in rails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/ Form excerpt | |
= f.button t(".save"), class: "btn" | |
= f.button t(".publish"), class: "btn", name: "publish" | |
= f.button t(".test"), class: "btn", name: "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultibuttonFormConstraint | |
def initialize(button_name) | |
@button_name = button_name | |
end | |
def matches?(request) | |
request.params.key? @button_name | |
end | |
end | |
# Add new routes with appropriate constrains | |
resources :subscriptions do | |
patch '' => "subscriptions#publish", on: :member, constraints: MultibuttonFormConstraint.new(:publish) | |
patch '' => "subscriptions#test", on: :member, constraints: MultibuttonFormConstraint.new(:test) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SubscriptionController < ApplicationController | |
# CRUD | |
def update | |
# process REST update | |
end | |
def publish | |
# process publish action | |
end | |
def test | |
# process test action | |
end | |
# ... | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment