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 !