0

Useful additional info: I am using the decent_exposure gem so this might be the issue - correcting the code below:

expose(:get_filter_tags) do
  if params[:filter_tag_names]
    filter_tag_names = Array(params[:filter_tag_names].split(" "))
    filter_tags = Array.new
    filter_tag_names.each do |f|
      t = Tag.find_by_name(f)
      filter_tags << t
    end 
  end
end

So, something funny happens when I call this in the view:

query string ?utf8=✓&filter_tag_names=test

<% get_filter_tags.each do |ft| %>
  <%= ft.name %> 
<% end %>

Error message: undefined method `name' for "test":String

Why is this trying to call name on a string not a Tag object? If I put the following in the view, and have jut one filter_tag_names item

def getfiltertag
  Tag.find_by_name(params[:filter_tag_names]) 
end

#view
<%= getfiltertag.name %>

query string: ?utf8=✓&filter=test

like above then I can call name just fine, so obviously I am doing something wrong to get an array of strings instead of objects. I just don't know what. Any suggestions?

2
  • 1
    You might want to change filter_tag_names = Array(params[:filter_tags].split(" ")) to filter_tag_names = params[:filter_tags].split(" ") Commented Aug 8, 2011 at 22:00
  • the funny thing was the code worked if I put it in the INDEX method but not in the DECENT EXPOSURE block ... but anyway, Chuck posted a better way to do it, below... Commented Aug 8, 2011 at 22:15

1 Answer 1

4

Your problem is that each returns self — so if you write filter_tag_names.each, it returns filter_tag_names. You could fix this by explicitly returning filter_tags, but more idiomatically, you could just rewrite it as:

expose(:get_filter_tags) do
  if params[:filter_tag_names]
    filter_tag_names = Array(params[:filter_tag_names].split(" "))
    filter_tag_names.map {|f| Tag.find_by_name(f) } 
  end
end

Just as an aside, this method will return nil if there aren't any filter tag names. You may want to do that, or you might want to return an empty collection to avoid exceptions in the calling code.

Sign up to request clarification or add additional context in comments.

4 Comments

Oh gosh, as usual, within minutes StackOverflow solves a problem I've noodled on for far more hours than it was worth - but at least I learned things along the way :-) ...thanks Chuck this is exactly what I needed to do!
Thanks for introduction to decent exposure, I found railscasts.com/episodes/259-decent-exposure which talks about it.
Hi Rubish ..."decent_exposure" is a gem which avoids the need to pass instance variables to the view, instead creating methods via "expose" in your controller that the view can call... it's really quite nice... see Railscasts #259 or read the Asciicasts version or check the gem documentation on Rubygems
hah - you beat me to it - Google got there for you faster than I could type, I am guessing!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.