我正在使用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} %>
我认为它不希望&lt;%=%&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
答案 0 :(得分:0)
我不熟悉演示者,但有一件事我可以注意到description
类中的Presenter
方法出错:你没有正确关闭字符串。
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} %>