3

I have problem with Rails destroy action. I have fully working message system. User can send, read messages but can't delete it. My Message Controller is:

class MessagesController < ApplicationController
 before_filter :set_user
 def index
   if params[:mailbox] == "sent"
    @messages = @user.sent_messages.page(params[:page])
   elsif params[:mailbox] == "inbox"
    @messages = @user.received_messages.page(params[:page])
   end
 end

 def new
  @message = Message.new
  @reply_to = User.find_by_id(params[:reply_to])
  unless @reply_to.nil?
    @message.recepient_id = @reply_to.id
  end
 end

 def create
  @message = Message.new(message_params)
  @message.sender_id = @user.id
  if @message.save
   flash[:notice] = "Message has been sent"
   redirect_to user_messages_path(current_user, :mailbox=>:inbox)
  else
   render :action => :new
  end
 end

 def show
  @message = Message.readingmessage(params[:id],@user.id)
 end
 def destroy
  @message = Message.find(params[:id])
  @message.destroy
 end

private

 def message_params
  params.require(:message).permit(:sender_id, :recepient_id, :body, :subject, :read_at)
 end

 def set_user
  @user = current_user
 end
end

My Routes are:

resources :users do
  resources :messages 
end

In view I have link to delete(which is not working):

 %h2 Your Inbox
- if @messages.size == 0
  %h1 No messages in your Inbox
- else
  %table.table.table-bordered
    %tr
      %th Subject
      %th Sender
      %th Sent At
    - for message in @messages
      %tr
        %td
          - if message.read?
            = link_to h(message.subject), user_message_path(@user, message)
          - else
            = link_to "#{h(message.subject)} (unread)", user_message_path(@user, message)
        %td= "#{message.sender.name}"
        = link_to "Delete Message", @message, method: :delete, data: {confirm: "You sure?"}

When i Click this link i have following error:

No route matches [DELETE] 

rake routes for Messages:

         user_messages GET    /users/:user_id/messages(.:format)          messages#index
                       POST   /users/:user_id/messages(.:format)          messages#create
      new_user_message GET    /users/:user_id/messages/new(.:format)      messages#new
     edit_user_message GET    /users/:user_id/messages/:id/edit(.:format) messages#edit
          user_message GET    /users/:user_id/messages/:id(.:format)      messages#show
                       PATCH  /users/:user_id/messages/:id(.:format)      messages#update
                       PUT    /users/:user_id/messages/:id(.:format)      messages#update
                       DELETE /users/:user_id/messages/:id(.:format)      messages#destroy

Any ideas? :)

3
  • You want message instead of @message.
    – bgates
    Aug 21, 2013 at 7:37
  • to make delete method work, you must have javascripts loading correctly. see in your browser that in firebug or chrome developers tools console that all the files are loading correctly. And could you paste your rake routes command output? Aug 21, 2013 at 7:37
  • When i change @mesaage to message i have the undefined method `message_path' error. Aug 21, 2013 at 7:40

3 Answers 3

10

This is because you are using nested route for messages in routes file:

It will generate routes in association with user. So either change your path in link

= link_to "Delete Message", user_message_path(message.user, message), method: :delete, data: {confirm: "You sure?"}

Or,

define a seperate route for messages if you want to delete messages without user object:

resources :messages 

now, u will be able to use message routes individually:

= link_to "Delete Message", message, method: :delete, data: {confirm: "You sure?"}
4
  • when I use: = link_to "Delete Message", user_message_path(message.user, message), method: :delete, data: {confirm: "You sure?"} i have the undefined method user' for #<Message:0x007f72c4cedf90> error. When I use: = link_to "Delete Message", user_message_path(@message.user,@message), method: :delete, data: {confirm: "You sure?"} i have the :undefined method user' for nil:NilClass. WTF? Aug 21, 2013 at 8:46
  • @MateuszUrbański do u have association between user and message. Can u paste your message.rb file.
    – Aman Garg
    Aug 21, 2013 at 8:53
  • Association is ok, because user can send and read messages, I writes tests for it and it's ok. message.rb: belongs_to :sender, class_name: 'User', primary_key: 'id', foreign_key: 'sender_id' belongs_to :recepient, class_name: 'User', primary_key: 'id', foreign_key: 'recepient_id' Aug 21, 2013 at 8:58
  • In this case, u may use message.sender instead of message.user. But better will be to write seperate route for messages also outside users which i have mentioned in second approach in my answer.
    – Aman Garg
    Aug 21, 2013 at 9:07
3

Because you have used nested routes, you need to pass two arguments in link_to, i.e. both user instance and message instance. And write the named route explicitly.

Like this:

= link_to "Delete Message", user_message_path(@user, @message), \
  method: :delete, data: {confirm: "You sure?"}
2
  • Thank for help but now I have the same error: No route matches [DELETE] "/users/1-jan-kowalski/messages" Aug 21, 2013 at 8:22
  • @MateuszUrbański, is the html generated correct? i.e. a form with method DELETE?
    – Billy Chan
    Aug 21, 2013 at 8:31
1

as its nested path, you need to pass user object as well..

= link_to "Delete Message", user_message_path(@message.user,@message), method: :delete, data: {confirm: "You sure?"}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.