Skip to content

Instantly share code, notes, and snippets.

@igrabes
Created March 19, 2012 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igrabes/2114285 to your computer and use it in GitHub Desktop.
Save igrabes/2114285 to your computer and use it in GitHub Desktop.
Adding permissions to mixtapes
#Mixtape Controller
def share
@mixtape = Mixtape.find(params[:id])
@shared_user = User.find(params[:user])
@shared_permission = params[:permission].to_sym
@mixtape.add_user(@shared_user, @shared_permission) if @mixtape.user_mixtapes.find_by_user_id_and_permission(@shared_user,@shared_permission)
redirect_to :back, :notice => "Added #{@shared_user.email} with the permission #{@shared_permission}"
end
#Mixtapes Show View (form only)
<% permission_types = UserMixtape::Permissions.map{|a| a } %>
<%= form_tag mixtapes_share_path(@mixtape) do %>
<%= select_tag("user", options_from_collection_for_select(@users, "id", "email")) %>
<%= select_tag("permission", options_from_collection_for_select(permission_types, :first, :first)) %>
<%= submit_tag("Share") %>
<% end %>
#User_mixtape model
class UserMixtape < ActiveRecord::Base
Permissions = {:owner => 0, :collaborator => 1, :listener => 2}
...
end
#routes.rb (relevant)
post 'mixtapes/:id/share' => 'mixtapes#share', :as => "mixtapes_share"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment