trying to setup a basic join :has_many :through
I have a basic Users / Memberships / Groups
I want a user to navigate to the Groups#show if the user is logged in they are shown a "Join" button, if the user is not logged they are offered a link to the login/registration.
I can handle the if logged in stuff using devise. The piece I don't know how to do is the Join... The real piece I am trying to figure out is the view code I think. I don't need anyone to write the code. just point me in the right direction and I'll figure it out...
I have all the basic code for this setup. The the tables are created, the models exist... the relationship is instantiated.
My current membership controller
def create
@membership = current_user.memberships.build(:group_id => params[:group_id])
if @membership.save
flash[:notice] = "You have joined this group."
redirect_to :back
else
flash[:error] = "Unable to join."
redirect_to :back
end
end
def destroy
@membership = current_user.memberships.find(param开发者_Go百科s[:id])
@membership.destroy
flash[:notice] = "Removed membership."
redirect_to :back
end
end
any direction would be great
You say you don't want the actual code, so here are a few pointers:
- You need a form.
- This form needs to post to the
create
action you've shown in your question. - You need to post the correct
group_id
.
Do you need any more pointers? Check out the Rails Guides, they're pretty good!
精彩评论