有一个消息控制器:
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
替换为不执行此操作的Message.new
,仍然是同样的问题。我还尝试删除resources :messages
以外的resources :chats
,但这并不正确,因为聊天有很多消息,而消息属于聊天。你知道这个表格对我有用吗?
编辑:聊天控制器:
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)
答案 0 :(得分:0)
无法看到您宣布@message
的位置?
-
错误似乎来自:
= render 'messages/form'
......有:
= simple_form_for @message do |f|
-
除了在部分中使用@instance_variables
是不好的做法之外,在这种情况下,您需要明确声明@message
才能识别它。
我实际上认为你可以通过将嵌套资源传递给messages/form
部分来解决问题:
#app/views/messages/_form.html.erb
= simple_form_for [@chat, @chat.messages.new] do |f|
(顺便说一下,所有nilClass
错误基本上都意味着你引用了一个尚未声明的变量。