Mel
Mel

Reputation: 2687

Rails 5 - how to write a view helper

I am trying to learn how to use helpers in my Rails 5 app.

I have an org_requests model, which has attributes to hold the date on which various steps are taken. I'm trying to use the controller to update those attributes when the relevant controller action is saved.

For example, I have an attribute called :approved_at. In my controller I have an action called approved with:

def approved
    @org_request = current_user.organisation.org_requests.find(params[:id])

    if @org_request.state_machine.transition_to!(:approved)
      @org_request.update_attribute(approved_at: Time.zone.now)
      flash[:notice] = "You've added this member."
      redirect_to org_requests_path
    else
      flash[:error] = "You're not able to manage this organisation's members"
      redirect_to :index
    end
  end

I think this line:

 @org_request.update_attribute(approved_at: Time.zone.now)

should save the time now, when the approved action is saved.

In my view, I'm trying to render the time at which the relevant actions were taken. To do that, I'm trying to write:

<% @org_requests.each do |org_req| %>
<%= text_for_time_of_status_change(org_req.current_state) %>         

I then have a helper that has:

def text_for_time_of_status_change(current_state)
        case current_state
          when 'requested'
            org_request.requested_at.try(:strftime, ' %l:%M  %e %B %Y')
          when 'approved'
            org_request.approved_at.try(:strftime, ' %l:%M  %e %B %Y')
          when 'rejected'
                org_request.rejected_at.try(:strftime, ' %l:%M  %e %B %Y')
          when 'removed'
            org_request.removed_at.try(:strftime, ' %l:%M  %e %B %Y')
        end
    end

I'm not able to give my helper an org_request like so (because it won't accept the '.'):

def text_for_time_of_status_change(org_request.current_state)

This isn't right. I'm not even sure whether I need to be using '@' in front of org_request (although I get an error when I try that).

Can anyone see what I need to do in order to use a view helper?

These articles: http://6ftdan.com/allyourdev/2015/01/28/rails-helper-methods/ seem to suggest that the helpers aren't meant to be used for presentation logic. Maybe they're meant for some other types of actions. I'm not sure I'm even trying to use the helper for the proper purpose.

Upvotes: 0

Views: 1475

Answers (2)

Oleh  Sobchuk
Oleh Sobchuk

Reputation: 3722

you have to fix your helper and would be better to use partial in your case:

helper:

def text_for_time_of_status_change(org_request)
  case org_request.current_state
  when 'requested'
    org_request.requested_at.try(:strftime, ' %l:%M  %e %B %Y')
  when 'approved'
    org_request.approved_at.try(:strftime, ' %l:%M  %e %B %Y')
  when 'rejected'
    org_request.rejected_at.try(:strftime, ' %l:%M  %e %B %Y')
  when 'removed'
    org_request.removed_at.try(:strftime, ' %l:%M  %e %B %Y')
  end
end

than create partial:

_requested.html.erb

<%= text_for_time_of_status_change(org_request) %>

than in view use collection in partial:

<%= render partial: 'requested', collection: @org_requests, as: org_request %>

Upvotes: 0

Paweł Dąbrowski
Paweł Dąbrowski

Reputation: 754

You have to update text_for_time_of_status_change method and pass org_req instead of org_req.current_state. You are getting error because you want to access org_request variable but didn't pass it to the helper method. After this change don't forget to update helper method code so you call org_request.current_state instead of current_state in case

Upvotes: 1

Related Questions