尝试将消息系统实施到rails app时有关路由的错误消息

时间:2015-03-21 11:26:56

标签: ruby-on-rails ruby routes message nested-routes

我正在尝试在我的rails项目中实现一个消息系统,但过去几天我一直被困住.. ..

1)到目前为止我做了什么

db / schema文件中的

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

在config / routes

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

在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

现在我遇到了错误: 1)在我views/convocations/edit的某个时刻我已经

   <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>

当我点击我得到的按钮时:

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

这很奇怪,因为我不知道为什么我需要messages_path来做我想做的事情......但我目前通过添加到我的路线来解决问题:

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

然后我可以进入消息创建页面 在views/messages/new中有一个简单的形式

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

不要担心它几乎结束了:)

当我点击提交btn时,我得到错误:

No route matches [POST] "/messages"
好吧,我没有得到它,因为我有一条匹配的路线:

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

所以我现在卡住了...感谢那个有勇气阅读这一切的人!

0 个答案:

没有答案