【问题标题】:Rails - using presenters in formsRails - 在表单中使用演示者
【发布时间】:2016-02-24 08:46:45
【问题描述】:

我正在使用 Rails 4 制作应用程序。我使用简单的形式。

我正在制作在表单输入上显示略有不同标签的演示者。

我使用它们如下:

形式:

<%= f.input :description, :as => :text, :label => " <%= @project_presenter.description %> ", :input_html => {:rows => 10} %>

在演示者中:

class ProjectPresenter

    def initialize(project, profile)
        @project = project
        @profile = user.profile
    end

    def description

        if student?
            "What does the project involve? "
        elsif sponsor?
            "Describe the project. "
        elsif educator?
            "What does the project involve? How will students be involved in the project? "
        elsif researcher?
            "What's involved in the project? Describe the issues to be addressed."
        end
    end 

当我尝试这个时,我得到一个指向这一行的语法错误:

&lt;%= f.input :description, :as =&gt; :text, :label =&gt; " &lt;%=@project_presenter.description %&gt; ", :input_html =&gt; {:rows =&gt; 10} %&gt;

我认为它不希望在演示者周围出现 标记。

您如何在表单中使用演示者?

Presenter 定义为:

class ProjectPresenter

    def initialize(project, profile)
        @project = project
        @profile = user.profile
    end

    def description
    ....
    end 

用户模型中的关联是:

  has_many :articles
  has_many :authentications, :dependent => :delete_all

  has_many :comments

  belongs_to :organisation
  has_one :profile
  has_many :qualifications
  has_many :identities

  has_many :trl_assessments, as: :addressable

  has_and_belongs_to_many :projects

【问题讨论】:

  • 您能否显示错误和随之而来的堆栈跟踪,您的表单 (f) 是绑定到演示者还是活动记录模型?考虑在表单标签之前使用&lt;%= debug @project_presenter %&gt;&lt;%= debug @project %&gt; - guides.rubyonrails.org/debugging_rails_applications.html
  • 是的,标签值不应该使用 erb 标签,它已经在 erb 块中 - 这两者都应该是有效的:&lt;%= f.input :description, :as =&gt; :text, :label =&gt; @project_presenter.description, :input_html =&gt; {:rows =&gt; 10} %&gt; 或(在这种情况下不需要但有效)&lt;%= f.input :description, :as =&gt; :text, :label =&gt; "#{@project_presenter.description}", :input_html =&gt; {:rows =&gt; 10} %&gt;

标签: ruby-on-rails forms presenter


【解决方案1】:

我不熟悉presenter,但我能注意到的一件事是Presenter 类中的description 方法中存在错误:您没有正确关闭字符串。

def description
  if student?
    "What does the project involve?"
  elsif sponsor?
    "Describe the project." 
  elsif educator?
    "What does the project involve? How will students be involved in the project? "
  elsif researcher?
    "What's involved in the project? Describe the issues to be addressed."
  end
end

而不是你现在拥有的:

def description
  if student?
    "What does the project involve? 
  elsif sponsor?
    "Describe the project. 
  elsif educator?
    "What does the project involve? How will students be involved in the project? "
  elsif researcher?
    "What's involved in the project? Describe the issues to be addressed."
  end
end

另外,在您看来:

<%= f.input :description, :as => :text, :label => " <%= @project_presenter.description %> ", :input_html => {:rows => 10} %>

由于您已经在erb 标记中调用@project_presenter.description,因此您无需在此处指定另一个&lt;%= %&gt;。如果您在这里尝试实现的是字符串插值,那么您只需将其称为:"#{@project_presenter.description}"

<%= f.input :description, :as => :text, :label => "#{@project_presenter.description}", :input_html => {:rows => 10} %>

同时,我认为您应该可以直接调用@project_presenter.description,无需插值如下:

<%= f.input :description, :as => :text, :label => @project_presenter.description, :input_html => {:rows => 10} %>

【讨论】:

  • 您好 Sunny,感谢您的光临。我实际上删除了实际演示者的大部分文本,以减少在这个问题上占用的空间,并删除了结尾的“。它们在代码中。
  • 我上面解释的插值的其他用途呢?
  • 我试过“#{@project_presenter.description}” - 但我得到一个无方法错误。当我尝试第二个建议时,它将演示者参考打印为文本
  • no_method 错误是什么?它指向哪个方法?
  • nil:NilClass 的未定义方法“描述”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-09
  • 2011-12-17
  • 2015-03-09
  • 2016-04-09
  • 1970-01-01
相关资源
最近更新 更多