Skip to content

Instantly share code, notes, and snippets.

@scarfacedeb
Last active January 18, 2023 22:35
Show Gist options
  • Save scarfacedeb/03ba3bf5f60b1c043d12 to your computer and use it in GitHub Desktop.
Save scarfacedeb/03ba3bf5f60b1c043d12 to your computer and use it in GitHub Desktop.
Form with multiple button in rails
/ Form excerpt
= f.button t(".save"), class: "btn"
= f.button t(".publish"), class: "btn", name: "publish"
= f.button t(".test"), class: "btn", name: "test"
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
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