【发布时间】:2015-12-10 10:42:20
【问题描述】:
有一个消息控制器:
class MessagesController < ApplicationController
before_filter :load_chat
def index
@messages = @chat.messages
end
def create
@message = @chat.messages.build(params[:message])
if @message.save
redirect_to @chat, notice: "Message sent"
else
render :new
end
end
private
def load_chat
@chat = Chat.find(params[:chat_id])
end
end
这是我的视图文件:
%h1 = @chat.name
= content_tag :div, id: 'messages', data: {url: chat_messages_path(@chat)} do
= render @chat.messages
%h2 New message
= render 'messages/form'
这是我的消息/表单:
= simple_form_for @message do |f|
= f.input :text
= f.submit 'Send message', class: 'btn btn-success'
最后是我的 routes.rb 文件:
resources :chats, only: [:index, :show, :new, :create] do
resources :messages
end
所以问题是当我打开聊天/显示链接时收到一个错误undefined method model_name for nil:NilClass,因为正在呈现一种新消息的形式。我试图用不能完成工作的Message.new 替换@message,仍然是同样的问题。我还尝试在resources :chats 之外删除resources :messages,这起到了作用,但这是不正确的,因为聊天中有很多消息并且消息属于聊天。你知道这个表格应该如何为我工作吗?
编辑:聊天控制器:
class ChatsController < ApplicationController
before_action :find_chat, :authenticate_user!
def index
@chats = current_user.chats
end
def show
@chat = current_user.chats.find(params[:id])
end
def new
@chat = current_user.chats.new
end
def create
@chat = current_user.chats.new chat_params
if @chat.save
flash[:notice] = 'chat asked'
redirect_to chats_path
else
render 'new'
end
end
def edit
end
def destroy
@chat.destroy!
flash[:notice] = 'chat deleted'
redirect_to chats_path
end
def update
if @chat.update_attributes chat_params
flash[:notice] = 'chat saved'
redirect_to chat_path
else
redirect_to edit_chat_path(@chat)
end
end
private
def chat_params
params.require(:chat).permit :name, :user_id
end
def find_chat
@chat = current_user.chats.find(params[:id]) if params[:id]
end
end
还有我的错误日志:
ActionView::Template::Error (undefined method `model_name' for nil:NilClass):
1: = simple_form_for @message do |f|
2: = f.input :text
3: = f.submit 'Send message', class: 'btn btn-success'
4: app/views/messages/_form.html.haml:1:in `_app_views_messages__form_html_haml___2629708334715527453_82911540' app/views/chats/show.html.haml:9:in `_app_views_chats_show_html_haml__2381438814653998256_88403540'
Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (10.9ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (3.9ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (3.2ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/actionpack-4.2.4/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (35.7ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_markup.html.erb (0.6ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_inner_console_markup.html.erb within layouts/inlined_string (0.5ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/_prompt_box_markup.html.erb within layouts/inlined_string (0.5ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/style.css.erb within layouts/inlined_string (0.9ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/console.js.erb within layouts/javascript (23.0ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/main.js.erb within layouts/javascript (0.5ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/error_page.js.erb within layouts/javascript (0.5ms) Rendered /home/titas/.rvm/gems/ruby-2.2.1/gems/web-console-2.2.1/lib/web_console/templates/index.html.erb (42.8ms)
【问题讨论】:
-
请贴出
ChatsController的源代码,以及你的错误日志。 -
@Aetherus 已更新 :)
-
将 @chat.messages.build(params[:message]) 更改为 @chat.messages.new(params[:message]) 和 simple_form_for @message do |f| to simple_form_for :message, url: 你的 url 做 |f|
标签: ruby-on-rails ruby activerecord haml simple-form