So I am making a chat app, and I want users to be able to leave chat rooms. This would be done by posting a delete request to a route like
has_many :chat_rooms, through: chat_room_users
has_many :users, through chat_room_users
You're overcomplicating it.
Instead of passing the user id via the URL you should instead get it through the session or a token (if its an API app).
Rule of thumb: resources should never be nested more than 1 level deep. A collection may need to be scoped by its parent, but a specific member can always be accessed directly by an id, and shouldn’t need scoping (unless the id is not unique, for some reason). http://weblog.jamisbuck.org/2007/2/5/nesting-resources
This is just a loose example of how to solve this:
# routes.rb resources :chat_rooms do member do post :join delete :leave end end
class User has_many :chat_room_users has_many :chat_rooms, though: :chats end class ChatRoomUser belongs_to :user belongs_to :chatroom end class ChatRoom has_many :chats has_many :users, though: :chats end
Putting this in
UsersController is pretty questionable. I would instead place it in
class ChatroomsController # ... # POST /chat_rooms/:chat_room_id/join def join @chat_room = ChatRoom.find(params[:id]) @chat = current_user.chat_room_users.new(chat_room: @chat_room) if @chat_room.create # ... else # ... end end # DELETE /chat_rooms/:chat_room_id/leave def leave @chat_room = ChatRoom.find(params[:id]) @chat = current_user.chat_room_users.find_by(chat_room: @chat_room) @chat.destroy end end
<%= button_to 'Join', join_chat_room_path(@chat_room), method: :post %> <%= button_to 'Leave', leave_chat_room_path(@chat_room), method: :delete %>