0

I have some ruby code in a view and it works great but I know that's not the correct way to do it so want to put it into a helper method (thanks @SimoneCarletti). I'm having some difficulty with that and usually I find people on here 10000x smarter than me who are excellent Rubyists.

Here is the code that I want to put into a helper

product_feed_item.voters_who_voted.map(&:full_name).join(("<br />").html_safe)

Here is the method below in my user model to get the user's product_feed

def product_feed
  Design.from_users_followed_by(self).to_a.concat Product.from_users_followed_by(self).to_a
end

The controller for this view is

@product_feed_items = current_user.product_feed
@product_feed_items.sort! {|t1, t2| t2.created_at <=> t1.created_at}

respond_to do |format|
  @product_feed_items = @product_feed_items.paginate(:page => params[:page], :per_page => 20)
  format.html
  format.json { render json: @product_feed_items }
end

and the View is

<%= render partial: "shared/product_feed_item", collection: @product_feed_items %>

Where the partial contains the first block of code I want to extract into a method.

How can I put this into a method?? If you need me to clarify or add anything to help please just let me know.

Thanks in advance for helping! Happy Holidays!

2 Answers 2

1

Its as simple as below unless i am missing something

def voter_full_names(product_feed_item)
  product_feed_item.voters_who_voted.map(&:full_name).join(("<br />").html_safe
end

And in your view

<%= voter_full_names(product_feed_item)%>
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! I did something SO close to that but missed one part lol
0

You can try to use decorator. With Draper decorators, you can wrap your models with presentation-related logic to organize.

https://github.com/drapergem/draper

Comments

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.