接收ActionView :: Template :: Error(未定义的方法'model_name'为nil:NilClass)Denshobato

时间:2018-01-04 22:12:54

标签: ruby-on-rails ruby

我正在使用Denshobato进行双模式聊天功能。我遇到的问题是Denshobato可能需要 conversations_controller messages_controller - 我目前正在将这两个控制器用于我项目中的不同功能。相反,我决定使用 discussion_controller discussion_messages_controller 。当我尝试在employees_meetup_controller的索引中呈现讨论的表单时。我收到未定义的方法'model_name'错误。我写的代码如下。如何设置Denshobato接受我的discussion_controller和discussion_message_controller别名?顺便说一下,我正在使用两个宝石来实现这个功能, denshobato_chat_panel denshobato

discussions_controller.rb

class DiscussionsController < ApplicationController
  before_action :current_account_signed_in?

  def index
    @discussions = current_account.my_conversations.includes(:sender)
    @trash = current_account.trashed_conversations
  end

  def create
    @discussion = current_account.hato_conversations.build(discussion_params)
    if @discussion.save
      redirect_to discussion_path(@discussion)
    else
      redirect_to :discussions, notice: @discussion.errors
    end
  end

  def destroy
    @discussion = Denshobato::Conversation.find(params[:id])
    redirect_to :discussions if @discussion.destroy
  end

  def show
    @discussion = Denshobato::Conversation.find(params[:id])
    redirect_to :discussions, notice: "You can't join this conversation" unless user_in_conversation?(current_account, @discussion)
    @discussion_message_form = current_account.hato_messages.build
    @discussion_messages = @discussion.messages.include(:author).page(params[:page]).per(25)
  end

  %w(to_trash from_trash).each do |name|
    define_method name do
      room = Denshobato::Conversation.find(params[:id])
      room.send(name)
      redirect_to :discussions
    end
  end

  private

  def discussion_params
    params.require(:denshobato_conversation).permit(:sender_id, :sender_type, :recipient_id, :recipient_type)
  end
end

_discussions_form.html.slim

= simple_form_for @discussion, url: :discussions do |form|
  = fill_conversation_form(form, user)
  = form.submit 'Start Discussion', class: 'btn btn-primary'

discuss_controller(index.html.slim)

- if @trash.any?
  h1 Trash
  - @trash.each do |room|
- @discussions.each do |room|
  = interlocutor_image(room.recipient, user.user_profile.avatar, 'imag-circle')
  = link_to "Discussion with: #{interlocutor_info(room.recipient, :first_name, :last_name)}", discussion_path(room)
  p = room.messages.last.try(:body)

  = interlocutor_image(room.messages.last.try(:author),
          user.user_profile.avatar, 'img-circle')
  - if room.messages.last.present?
  p = "Last message from: #{message_from(room.messages.last, :first_name, :last_name)}"
  = button_to 'End Conversation', discussion_path(room),
          method: :delete, class: 'btn btn-outline-danger'
  = button_to 'Move to Trash', to_trash_path(id: room),
          class: 'btn btn-warning', method: :patch
  = button_to 'Move from Trash', from_trash_path(id: room),
          class: 'btn btn-warning', method: :patch
  hr

discuss_controller(show.html.slim)

= simple_form_for @discussion_message_form, url: :discussion_messages do |form|
  = form.input :body, class: 'form-control'
  = fill_message_form(form, current_account, @discussion)
  = form.submit 'Send message', class: 'btn btn-primary'
  = render_denshobato_messages(@discussion, current_user)
  - @discussion_messages.each do |msg|
    = msg.body
    hr

discussion_messages_controller.rb

class DiscussionMessagesController < ApplicationController
  before_action :current_account!

  def create
    discussion_id = params[:denshobato_message][:discussion_id]
    @discussion_message = current_account.send_message_to(discussion_id, discussion_params)

    if @discussion_message.save
      @discussion_message.send_notification(discussion_id)
      redirect_to discussion_path(discussion_id), notice: 'Message sent'
    else
      redirect_to discussion_path(discussion_id), notice: @discussion_message.errors
    end
  end

  private
  def discussion_params
    params.require(:denshobato_message).permit(:body, :author_id, :author_type)
  end
end

employer_meet_up_controller.rb

- @users.each do |user|
  = user.user_full_name
  - unless conversation_exists?(current_account, user)
    - if can_create_conversation?(current_account, user)
      - if user_in_black_list?(current_account, user)
        p 'This user in your blacklist'
        = button_to 'Remove from black list',
                remove_from_blacklist_path(user: user, klass: user.class.name), class: 'btn btn-info'
      - else
        = button_to 'Add to black list',
                black_list_path(user: user, klass: user.class.name), class: 'btn btn-danger'
        = render partial: 'discussions/discussion_form', :locals => {:title => "Discussion"}
  hr

user.rb

  def user_full_name
    first_name + " " + last_name
  end

  def full_name
  # denshobato method
    "#{first_name} #{last_name}"
  end

def image
    user.user_profile.avatar.url
  end

denshobato_for :user
  denshobato_chat_panel

employer.rb

 denshobato_for :employer
   denshobato_chat_panel

的routes.rb

  mount Denshobato::DenshobatoApi => '/'
  resources :discussions
  resources :discussion_messages
  patch :to_trash, to: 'discussions#to_trash', as: :to_trash
  patch :from_trash, to: 'discussions#from_trash', as: :from_trash
  post :black_list, to: 'blacklists#add_to_blacklist', as: :black_list
  post :remove_from_blacklist, to: 'blacklists#remove_from_blacklist', as: :remove_from_blacklist
  root 'home#index'

application_helper.rb

module ApplicationHelper

  def current_account
    current_user || current_employer
  end

  def current_account_signed_in?
    user_signed_in? || employer_signed_in?
  end

  def notification_count_for(user)
      Notification.unread_count(user)
  end

  def user_logged_in?
    !current_user.nil?
  end

  def activate_account_time_zone
    return yield unless current_account # nothing to do if no user logged in
    return yield unless current_account.time_zone && ActiveSupport::TimeZone[current_account.time_zone] # nothing to do if no user zone or zone is incorrect
    Time.use_zone(ActiveSupport::TimeZone[current_account.time_zone]) do
      yield
    end
  end
end

控制台日志

Processing by EmployerMeetUpController#index as HTML
  Employer Load (0.0ms)  SELECT  "employers".* FROM "employers" WHERE "employers"."id" = $1 ORDER BY "employers"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering employer_meet_up/index.html.slim within layouts/application
  User Load (1.0ms)  SELECT  "users".* FROM "users" ORDER BY "users"."first_name" ASC LIMIT $1 OFFSET $2  [["LIMIT", 46], ["OFFSET", 0]]
  Denshobato::Conversation Load (1.0ms)  SELECT  "denshobato_conversations".* FROM "denshobato_conversations" WHERE "denshobato_conversations"."sender_type" = 'Employer' AND "denshobato_conversations"."sender_id" = 1 AND "denshobato_conversations"."recipient_type" = 'User' AND "denshobato_conversations"."recipient_id" = 1 LIMIT $1  [["LIMIT", 1]]
  Denshobato::Blacklist Load (0.0ms)  SELECT "denshobato_blacklists".* FROM "denshobato_blacklists" WHERE "denshobato_blacklists"."blocker_type" = 'Employer' AND "denshobato_blacklists"."blocker_id" = 1 AND "denshobato_blacklists"."blocked_type" = 'User' AND "denshobato_blacklists"."blocked_id" = 1
  Rendered discussions/_discussion_form.html.slim (781.1ms)
  Rendered employer_meet_up/index.html.slim within layouts/application (827.2ms)
Completed 500 Internal Server Error in 1248ms (ActiveRecord: 2.0ms)



ActionView::Template::Error (undefined method `model_name' for nil:NilClass):
    1: = simple_form_for @discussion, url: :discussions do |form|
    2:   = fill_conversation_form(form, user)
    3:   = form.submit 'Start Discussion', class: 'btn btn-primary'

app/views/discussions/_discussion_form.html.slim:1:in `_app_views_discussions__discussion_form_html_slim__1021469361_78616992'
app/views/employer_meet_up/index.html.slim:12:in `block in _app_views_employer_meet_up_index_html_slim__524570216_75009660'
app/views/employer_meet_up/index.html.slim:1:in `_app_views_employer_meet_up_index_html_slim__524570216_75009660'
app/helpers/application_helper.rb:21:in `activate_account_time_zone'

1 个答案:

答案 0 :(得分:0)

您收到此错误是因为@discussion为零。检查您的控制器方法,确保它存在并正确通过。

您收到此错误的原因是ActiveRecord对象使用ActiveModel::Naming模块生成关联的URL帮助程序(如表单应发布到的位置,除非您另行指定)并形成帮助程序(比如你的字段名称的前缀)

有关ActiveModel::Naming http://api.rubyonrails.org/classes/ActiveModel/Naming.html

的更多信息