简体   繁体   中英

Rails 5 - Helper methods

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

I have a model called Proposal and another called User .

In my Proposal helper, I'm trying to write a helper to show the name and location of the parent user (proposals belong to users).

I have tried:

def text_for_proponent(proposal, user)
  return unless current_user.id == proposal.user.id
    render 'users/profiles/formal_name_title', user: @proposal.user
end

Then in my views/users/formal_name_title.html.erb, I have:

<%= @user.full_name.titleize %> · <%= @user.organisation.title.titleize %> · <%#= @user.addresses.first.country_name.titleize %>

Then in my proposals/show.html.erb, I have:

<h4><%= text_for_proponent(@proposal, @proposal.user) %></h4>

This isn't working. I can't find how to give the user instance to the helper to be used in populating the view.

Can anyone see where I'm going wrong?

The problem is that you're trying to access @proposal.user but @proposal is nil inside the helper. You should use the local variable proposal instead.

render 'users/profiles/formal_name_title', user: proposal.user

And, you should change your partial to use the local variable user .

<%= user.full_name.titleize %> · <%= user.organisation.title.titleize %> · <%= user.addresses.first.country_name.titleize %>

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