Bye
Bye

Reputation: 778

Rails: helper method output # instead of correct value

resource_helper.rb

 def show_checkbox resources
    resources.each do |resource|
      resource.name 
    end.join(' ').html_safe
  end

view

<%= show_checkbox resource %>

This code will output # ,I am sure the value inside is correct. But not sure why it output #

Upvotes: 0

Views: 29

Answers (1)

sevenseacat
sevenseacat

Reputation: 25049

You want to use resources.map, not resources.each.

each will return the value you're iterating over, not the content of the block - giving you something like #<Resource:34531231>. The rest is interpreted as a HTML tag, leaving you with just the # showing.

map will return the value of the block, turning an array of Resource objects into an array of string names, as you want.

Upvotes: 2

Related Questions