0

I am using Rails 4 to make an app. I use simple form for forms.

I am making presenters which display a slightly different label on form inputs.

I'm using them as follows:

In the form:

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

In the presenter:

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 

When I try this, I get a syntax error pointing at this line:

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

I think it doesn't want the <%= %> tags around the presenter.

How do you use presenters in forms?

Presenter is defined as:

class ProjectPresenter

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

    def description
    ....
    end 

Associations in the user model are:

  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
Mel
  • 2,481
  • 26
  • 113
  • 273
  • can you show the error and the stack trace that goes with it, also is your form (`f`) bound to the presenter or an activerecord model? consider using `<%= debug @project_presenter %>` and `<%= debug @project %>` before the form tag - http://guides.rubyonrails.org/debugging_rails_applications.html – house9 Nov 22 '15 at 16:33
  • and yes the label value should not be using erb tags, it is already in an erb block - these both should be valid: `<%= f.input :description, :as => :text, :label => @project_presenter.description, :input_html => {:rows => 10} %>` OR (not needed in this case but valid) `<%= f.input :description, :as => :text, :label => "#{@project_presenter.description}", :input_html => {:rows => 10} %>` – house9 Nov 22 '15 at 16:35

1 Answers1

0

I am not familiar with presenter, but one thing I can notice is that there is an error in your description method in the Presenter class: You are not closing your strings properly.

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

Instead of what you presently have:

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

Also, in your view:

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

Since you are already calling @project_presenter.description in an erb tag, you don't need to specify another <%= %> here. If what you are trying to achieve here is String interpolation, all you have to do is call it as: "#{@project_presenter.description}"

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

And at the same time, I think you should be able to call @project_presenter.description directly without interpolation as follow:

<%= f.input :description, :as => :text, :label => @project_presenter.description, :input_html => {:rows => 10} %>
x6iae
  • 4,074
  • 3
  • 29
  • 50