views:

111

answers:

2

I am at an absolute loss as to what I am doing wrong with the following code. I am trying to implement a messaging system within my application, but want it to be handle different types of messages. In this case, I want to create a "request" message of ':message_type => 1'.

Instead of using forms as I usually have, I want to make this instance the moment the link is clicked. Here is how I have it set up in the show erb file for "user":

<%=link_to "Send friend request", :action=>"request", :controller => "messages", :id => @user.id %>

and in the controller:

  def request
    @message = Message.new(:sender_id => current_user.id,:user_id => params[:id],:message_type => 1)
    if @message.save
        flash[:notice] = 'Message was successfully created.'
        redirect_to message_path(@message)
      else
        redirect_to message_path(@message)
      end
  end

This results in the following error message: undefined method `rewrite' for nil:NilClass with the trace looking like

 c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/whiny_nil.rb:52:in `method_missing'
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_controller/base.rb:634:in `url_for'
    (eval):16:in `message_path'
    app/controllers/messages_controller.rb:11:in `request'

I have used map.resources :messages in the routes.rb file, and done the appropriate :has_many and :belongs_to associations in the models of user and message.

EDIT: Something else to note is that the save IS succeeding, as once the root address is manually inputted into the address bar, the "flash" notice is shown saying that the save was made. Using the development console it is indeed there, so there's something messed up with the redirect.

A: 

Check your logs more closely, and you'll probably find that your save is failing. Not sure which line is #11, but I would guess that it's in your else block, which tries to build a path for a @message object with a nil ID (it hasn't been saved).

jdl
line 11 is the redirect_to in the if statement, not the else...
Jack
Change it to message_path(:id => @message.id) -- log the ID as well if that fails, and post what you get.
jdl
Also, your "else" condition will definitely fail if it's ever reached.
jdl
Here is the response from the server:Processing ApplicationController#request (for 127.0.0.1 at 2010-05-17 18:52:51) [GET] Parameters: {"action"=>"request", "id"=>"3", "controller"=>"messages"}NoMethodError (undefined method `rewrite' for nil:NilClass): (eval):16:in `message_path' app/controllers/messages_controller.rb:11:in `request' -e:2:in `load' -e:2Rendered rescues/_trace (73.0ms)Rendered rescues/_request_and_response (3.0ms)Rendering rescues/layout (internal_server_error)
Jack
+2  A: 

You might want to rename the action, I am quite sure that request means something in the controller.

Why dont you rename the action from request to create, and see if it helps.

So the code will be:

In the view

<%=link_to "Send friend request", :action=>"create", :controller => "messages", :id => @user.id %>

In the controller

 def create
    @message = Message.new(:sender_id => current_user.id,:user_id => params[:id],:message_type => 1)
    if @message.save
      flash[:notice] = 'Message was successfully created.'
      redirect_to message_path(@message)
    else
      redirect_to message_path(@message)
    end
  end
SamChandra
Wow. Thank you. This was a very frustrating ordeal. I wish those error messages were a bit more descriptive. That's pretty unlucky
Jack
This "else" block is still a bug waiting to happen, as I pointed out in my answer. I'm glad you got your immediate issue resolved though.
jdl
Yes, I actually just changed both redirect to "redirect_to :back" but thanks!
Jack