【问题标题】:Rails - NameError undefined local variable or method `conversation'Rails - NameError 未定义的局部变量或方法“对话”
【发布时间】:2016-02-04 19:45:07
【问题描述】:

Rails 新手在这里。我正在尝试这个 Mailboxer gem 并在我的网站上实施。我想创建一个收件箱系统,但不知何故它没有向我显示对话显示页面。它给了我以下错误:

对话中的名称错误#show C:/Sites/myinbox/app/views/conversations/show.html.haml 在哪里 第 6 行提出:

未定义的局部变量或方法`conversation'

我不知道哪里出错了。我正在关注有关如何设置它的教程 here,但我没有显示对话。

如果您能提供帮助,我将不胜感激!

show.html.haml

.row
  .spacer
  .col-md-6
    = link_to "Compose", new_conversation_path, class: "btn btn-success"
  .col-md-6.text-right
    - if conversation.is_trashed?(current_user) <!-- LINE 6 -->
      = link_to 'Untrash', untrash_conversation_path(conversation), class: 'btn btn-info', method: :post
    - else
      = link_to 'Move to trash', trash_conversation_path(conversation), class: 'btn btn-danger', method: :post, |
        data: {confirm: 'Are you sure?'}                                                                        |
.row
  .spacer
  .col-md-4
    .panel.panel-default
      .panel-body
        = render 'mailbox/folders'
  .col-md-8
    .panel.panel-default
      .panel-body
        = render partial: 'messages'
      .panel-footer
        / Reply Form
        = form_for :message, url: reply_conversation_path(conversation) do |f|
          .form-group
            = f.text_area :body, placeholder: "Reply Message", rows: 4, class: "form-control"
          = f.submit "Reply", class: 'btn btn-danger pull-right'

conversations_controller.rb

class ConversationsController < ApplicationController
  before_action :authenticate_user!

  def new
  end

  def create
    recipients = User.where(id: conversation_params[:recipients])
    conversation = current_user.send_message(recipients, conversation_params[:body], conversation_params[:subject]).conversation
    flash[:notice] = "Your message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

  def show
    @receipts = conversation.receipts_for(current_user).order("created_at ASC")
    # mark conversation as read
    conversation.mark_as_read(current_user)
  end


  def reply
    current_user.reply_to_conversation(conversation, message_params[:body])
    flash[:notice] = "Your reply message was successfully sent!"
    redirect_to conversation_path(conversation)
  end

  def trash
    conversation.move_to_trash(current_user)
    redirect_to mailbox_inbox_path
  end

  def untrash
    conversation.untrash(current_user)
    redirect_to mailbox_inbox_path
  end


  private

  def conversation_params
    params.require(:conversation).permit(:subject, :body,recipients:[])
  end

  def message_params
    params.require(:message).permit(:body, :subject)
  end
end

【问题讨论】:

  • 您需要将conversation 声明为@conversation。因为,视图只能访问实例变量。
  • 本教程的下一步(在此控制器中创建 conversation 辅助方法)应该会有所帮助。

标签: ruby-on-rails ruby


【解决方案1】:

show.html.haml 文件无法直接访问会话,因此您需要在会话控制器.rb 中的节目中创建一个实例变量,即@conversation = conversation。

【讨论】:

    【解决方案2】:

    对现有答案进行更多推理。

    暂时分解一下:if conversation.is_trashed?(current_user):

    if 在变量 conversation 上使用参数 current_user 调用方法 is_trashed 会产生一个真实值,然后继续

    变量conversation 来自哪里?如果您将视图视为一种方法,您会注意到一些事情 - 它不会在任何地方定义 conversation

    为了让您的视图能够从控制器中查看变量,您必须将其设为实例变量。在 ruby​​ 中,这是通过在其前面加上 @ 来完成的(是的,它有点语法魔术 - “常规”变量通过它们的封闭方法可见,但名称以 @ 开头的变量对整个实例可见) .

    【讨论】:

      【解决方案3】:

      在您的 application_controller.rb (app/controllers/application_controller.rb) 上,您必须这样做:

      class ApplicationController < ActionController::Base
        # [...]
        helper_method :mailbox, :conversation
      
        private
        # [...]
        def conversation
          @conversation ||= mailbox.conversations.find(params[:id])
        end
      
        protected
        # [...]
      end
      

      【讨论】:

      • 你能解释一下“这个”是什么意思吗?什么是相关部分,为什么它们解决了原来的问题?
      • 显示视图上的名称错误表明会话作为实例变量未定义......因此在其他修复该邮箱支持您在应用程序控制器上定义该方法时。如果你仔细看看它为涉及发送者和接收者的特定单个对话获取 id 的方法。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-19
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2016-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多