简体   繁体   中英

How to access a helper method in Rails?

I've got a rails app with a main_controller for providing content to the main page of my app. I want to access a collection of images so I created the following method in the controller:

def featured_illustrations
    #future implementation: get images where featred == true
    @featured_illustrations << Illustration.find(152)
    @featured_illustrations << Illustration.find(272)
    @featured_illustrations << Illustration.find(275)

    respond_to do |format|
      format.html
      format.json { render :json => @featured_illustrations }
    end
  end
helper_method :featured_illustrations

I have the following helper:

module MainHelper
  require MainController
  @featured_illustrations = MainController.featured_illustrations
end

And the following code in the featured_images partial:

<div class="featured">
  <% @featured_illustrations.each do |illustration| %>  
   <!-- somc code to print images -->
  <% end %>
</div>

I get this error when I try to view the page:

uninitialized constant MainHelper::MainController

I haven't worked with helpers before so any guidance is appreciated. Thanks.

UPDATE: I've removed the MainHelper but now I'm getting the following error when I try to load the page:

undefined method `each' for nil:NilClass
Extracted source (around line #2):

1: <div class="featured">
2:   <% featured_illustrations.each do |illustration| %>  
3:     <div class="feature-image" id="feature-one">152</div>
4:     <div class="feature-image" id="feature-two">272</div>
5:     <div class="feature-image" id="feature-three">275</div>

Any thoughts on the cause?

Easy answer: In the error you posted, it looks like you're not iterating over the correct variable. You've assigned your featured illustrations to an instance variable (they start with @) not a local variable which is what you're using.

Change:

<% featured_illustrations.each do |illustration| %>

To:

<% @featured_illustrations.each do |illustration| %>

However, I'm not exactly sure why you need a helper here if all you're doing is setting an instance variable to access in the partial.

In your featured_illustrations you're setting the instance variable. I'm not sure what action you're using when rendering your partial given the name is featured_images though.

I would suggest:

app/controllers/main_controller.rb

def featured_illustrations
  @featured_illustrations = Illustration.where(featured: true)

  respond_to do |format|
    format.html
    format.json { render :json => @featured_illustrations }
  end
end

app/views/main/featured_illustrations.html.erb

<div class="featured">
  <% @featured_illustrations.each do |illustration| %>  
    <!-- somc code to print images -->
  <% end %>
</div>

This is assuming that you have your routes file setup to correctly route to the main controller for the featured illustrations method. Something like:

match '/featured_illustrations/', to: 'main#featured_illustrations'

Finally, you might want to look at how Rails uses REST and resourceful routing in case you haven't already done so. You can find a lot in the Getting Started with Rails guide .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM