【发布时间】: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
当我尝试这个时,我得到一个指向这一行的语法错误:
<%= 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
【问题讨论】:
-
您能否显示错误和随之而来的堆栈跟踪,您的表单 (
f) 是绑定到演示者还是活动记录模型?考虑在表单标签之前使用<%= debug @project_presenter %>和<%= debug @project %>- guides.rubyonrails.org/debugging_rails_applications.html -
是的,标签值不应该使用 erb 标签,它已经在 erb 块中 - 这两者都应该是有效的:
<%= f.input :description, :as => :text, :label => @project_presenter.description, :input_html => {:rows => 10} %>或(在这种情况下不需要但有效)<%= f.input :description, :as => :text, :label => "#{@project_presenter.description}", :input_html => {:rows => 10} %>
标签: ruby-on-rails forms presenter