0

我正在使用 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 

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

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

我认为它不希望演示者周围有 <%= %> 标记。

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

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
4

1 回答 1

0

我对presenter不熟悉,但我能注意到的一件事是你descriptionPresenter课堂上的方法有一个错误:你没有正确地关闭你的字符串。

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} %>

由于您已经@project_presenter.description在调用erb标签,因此您无需<%= %>在此处指定另一个标签。如果您在这里尝试实现的是字符串插值,那么您所要做的就是将其称为:"#{@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} %>
于 2015-11-22T02:06:24.477 回答