0

I am trying to implement a message system into my rails project but Ive been stuck for the past few days.. .

1) What I ve done so far

in db/schema file:

create_table "messages", force: :cascade do |t|
  t.integer  "conversation_id"
  t.integer  "user_id"
  t.datetime "read_at"
  t.text     "content"
  t.datetime "created_at",      null: false
  t.datetime "updated_at",      null: false
end

create_table "conversations", force: :cascade do |t|
  t.integer  "user1_id"
  t.integer  "user2_id"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

in models/message.rb

class Message < ActiveRecord::Base
  belongs_to :conversation
  belongs_to :user

  validates_presence_of :conversation, :user, :content
end

in models/convcersation.rb

class Conversation < ActiveRecord::Base
  belongs_to :user1, class_name: "User"
  belongs_to :user2, class_name: "User"

  has_many :messages
  accepts_nested_attributes_for :messages
  validates_presence_of :user1, :user2
end

in config/routes

resources :convocations, only: [:edit, :update] do
  resources :messages, only: [:new, :create]
end

in controller/messages_controller.rb

class MessagesController < ApplicationController
  before_action :set_conversation, only: [:show, :reply, :reply_server]
  after_action :verify_policy_scoped, :only => :index
  after_action :verify_authorized, except:  [:new, :create]
  respond_to :js, only: :reply

  def new
    @message = Message.new
  end

  def create
    @conversation = Conversation.new
    @conversation.user1 = current_user
    @conversation.user2 =        Convocation.find(params[:convocation_id]).subscription.tournament.user
    @message = Message.new(message_params)
    @message.user = current_user
    @message.conversation = @conversation
    if @message.save
      redirect_to new_convocation_message_path
    else
      render :new
    end
  end

  def index
  end

  private

  def message_params
    params.require(:message).permit(:content)
  end
end

now here's the errors I get : 1) in my views/convocations/edit at some point Ive got

   <div class="modal-footer text-center">
      <p> Envoyez un message au juge arbitre pour lui donner vos dispos</p>
      <%= link_to "envoyer un message",new_convocation_message_path(@convocation), class: "btn btn-primary" %>
   </div>

when I click on the button I get :

undefined method `messages_path' for #<#<Class:0x007fec5c8feda0>:0x007fec5561b1f8>

this is strange as I don't know why I would need a messages_path for what I m trying to do... but I currently fix the problem by adding to my routes:

get "messages", to: "tournaments#index", as: "messages"

I am then able to get to the message creation page in views/messages/new which has a simple form like that

<%= simple_form_for [@convocation, @message] do |f| %>
  <%= f.input :content %>
  <%= f.button :submit, class: "btn btn-success" %>
<% end %>

Don't worry it's almost the end :)

When I click on the submit btn here's the error I get:

No route matches [POST] "/messages"

well I dont get it because I have a route that matches that:

convocation_messages_path   POST    /convocations/:convocation_id/messages(.:format)    messages#create

So im stuck right now... Thanks to the one who had the courage to read all this !

Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37
David Geismar
  • 3,152
  • 6
  • 41
  • 80
  • `new_convocation_message_path(@convocation)`, in this line of code. You don't have something like `conversation`. from where `convocation` came. WHere is your `Convocation` model – Sonalkumar sute Mar 21 '15 at 11:58
  • Well I probably need to explain a little more what my app does. A user can send a convocation to another user. A convocation is an "invitation" at a given date and hour. The recipient user can either accept or refuse the convocation. In case of refusal the Recipient can send a message to the convocation sender explaining why he cant come (this is why the "send a message" btn is inside the View/convocation/edit – David Geismar Mar 21 '15 at 13:16
  • The way I see it is that when the message is created when the user clicks on the submit button in views/message/new, a conversation is also created : thus in my message controller I also create a new conversation in the create method def create @conversation = Conversation.new – David Geismar Mar 21 '15 at 13:33
  • you can see the app's full code here : https://github.com/davidgeismar/tennis-match – David Geismar Mar 21 '15 at 13:34
  • `new_convocation_message_path(@convocation)` when you call this on click event, this means in `messages` controller there is a method as `new_convocation`. And As this is taking you to `messages` controller, you have to pass `message` instance like `message_path(@message)` – Sonalkumar sute Mar 21 '15 at 13:41
  • there is no new_convocation method in my message controller. I create new convocations in my convocation controller. the new_convocation_message_path(@convocation) is not linked to the creation of a convocation it is a just a nested route that takes you to the creation form of a message – David Geismar Mar 21 '15 at 14:04
  • run `rake routes` see is there any such route – Sonalkumar sute Mar 21 '15 at 14:12
  • there is such route(linked to message#new action) and the route actually works and takes me to the message form creation. The problem is when I try to create a message: I click the submit button in views/messages/new and I get the following error: No route matches [POST] "/messages" but when I run my rake routes I actually have a route that is linked to the message#create action: convocation_messages POST /convocations/:convocation_id/messages(.:format) messages#create I dont get why I end up with the previous error – David Geismar Mar 21 '15 at 17:35

0 Answers0