简体   繁体   English

Rails 5-辅助方法

[英]Rails 5 - Helper methods

I am trying to learn how to use helper methods in my Rails 5 app. 我正在尝试学习如何在Rails 5应用程序中使用辅助方法。

I have a model called Proposal and another called User . 我有一个名为Proposal的模型,另一个名为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). 在我的Proposal帮助器中,我试图编写一个帮助器以显示父用户的名称和位置(建议属于用户)。

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: 然后在我的views / users / formal_name_title.html.erb中,我有:

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

Then in my proposals/show.html.erb, I have: 然后在我的proposal / show.html.erb中,我有:

<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. 问题是您正在尝试访问@proposal.user@proposal在帮助@proposalnil You should use the local variable proposal instead. 您应该改用局部变量proposal

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

And, you should change your partial to use the local variable user . 并且,您应该更改局部变量以使用局部变量user

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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