Skip to content

Instantly share code, notes, and snippets.

@b1nary
Created March 24, 2017 08:48
Show Gist options
  • Save b1nary/e1c3cdede392b07ae61acfa443e1301c to your computer and use it in GitHub Desktop.
Save b1nary/e1c3cdede392b07ae61acfa443e1301c to your computer and use it in GitHub Desktop.
Foo Bar
class FooBarController < ApplicationController
before_action :set_stuff
def create
# same as below
@bar.foods.add(@foo)
end
def destroy
# remove beeing a method on the foo object here
@bar.foos.remove(@foo)
end
private
def set_stuff
@foo = Foo.where(id: params[:foo_id])
@bar = Bar.where(id: params[:bar_id])
end
end
...
resources :foobar, only: [:create, :destroy]
...
<%= link_to "Add", create_foobar_path(@foo, @bar) %>
<%= link_to "Remove", destroy_foobar_path(@foo, @bar), method: :destroy %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment