2016-09-01 22 views
0

我以前问此相关的问题,但没有发现任何帮助Rails 4 - how to use a helper method in an index viewRails 4 - 如何在索引视图中使用辅助方法?

林其在这方面同样的问题。在我的索引操作中使用帮助器方法一定会阻止我。

我有一个索引视图和一个显示视图。帮手在展会视图中正常工作。

在我的索引视图我有:

<% @eois.sort_by(&:created_at).each do |eoi| %> 
    <div class="col-md-3 col-sm-5 portfolioitem Scienza"> 
    <div class="portfolio-item text-center"> 
     <h4><%= link_to eoi.user.full_name %></h4> 
     <span>Interested<%= display_interest(eoi) %></span> 
     <%= link_to 'VIEW DETAILS', project_eoi_path(@project, eoi), :class=> "portfolio-item-view" %> 
    </div> 
    </div> 
<% end %> 

在我的节目的看法,我有:

<td><%= @eoi.user.full_name %></td> 
<td><%= @eoi.user.profile.organisation.try(:title) %></td> 
<td>Interested<%= display_interest(@eoi) %></td> 
<td><%= @eoi.created_at.try(:strftime, '%e %B %Y') %></td> 

在帮助我的文件,我有:

def display_interest(eoi) 
    if interested_in_contributing 
    'in contributing resources to this project team' 
    elsif interested_in_participating 
    'in participating in this project' 
    elsif interested_in_partnering 
    'in partnering with this project team' 
    elsif interested_in_granting 
    'in assessing this project for a grant' 
    elsif interested_in_investing 
    # elsif eoi.invest || @invest 
    'in investing in the outcomes of this project' 
    else 
    nil 
    end 
end 

# Depending on the type of interest, then figure out which questions need to be asked. 

# If the interest is in participation - there is no need to ask questions relating to asset requests 
# TODO - need a better way to check the js on participant_intrest to feed through this same channel 
def interested_in_participating 
    @eoi.participate || @participate 
end 

# If the interest is in contributing assets or partnering, need to check which assets are relevant to the interest 
def interested_in_contributing 
    @eoi.contribute || @contribute 
end 

怎么来的我不能使用索引视图中的帮助文件?

控制器:

def index 
    @eois = Project.by_user_id(current_user.id).find_by(id: params[:project_id]).try(:eois) || Eoi.none 
    policy_scope(@eois) 
end 

def show 
    @eoi = Eoi.find(params[:id]) 
    authorize @eoi 
end 
+0

“必须有东西阻止我在我的索引操作中使用帮助器方法” - 多数民众赞成在奇怪什么是你得到的错误? –

+0

未定义的方法'贡献'零:NilClass – Mel

+0

你应该传递参数给你的帮手,而不是试图依靠实例变量。 –

回答

0

试试这个

def display_interest(eoi) 
    if interested_in_contributing eoi 
    'in contributing resources to this project team' 
    . 
    . 


def interested_in_contributing eoi 
    eoi.contribute || @contribute 
end 
+0

当我在你的控制器中尝试 – Mel

+0

时,我得到了同样的错误如何在显示和索引操作中获得@eoi instace? –

+0

hmmm ...从你的助手中删除所有“@eoi”“interested_in_​​”并将它作为参数传递给它们。 –

0

错误说undefined method contribute on nil class这种方法被称为上@eoi检查,如果你有你的索引操作定义的@eoi实例变量。它应该是这个样子:

def index 
    @eoi = 'some value' 
end 

它可能是指你的show行动,但不是你的index行动。希望能帮助到你。

0

您应该将变量eoi作为参数传递给interested_in_participatinginterested_in_contributing方法。这是因为对于索引操作,实例变量是@eois,而不是@eoi。这是代码片段。

def interested_in_participating(eoi) 
    eoi.participate || @participate 
end 

# If the interest is in contributing assets or partnering, need to check which assets are relevant to the interest 
def interested_in_contributing(eoi) 
    eoi.contribute || @contribute 
end 

# Add parameter while calling the methods 
def display_interest(eoi) 
    if interested_in_contributing(eoi) 
    'in contributing resources to this project team' 
    elsif interested_in_participating(eoi) 
    'in participating in this project' 
    elsif interested_in_partnering 
    'in partnering with this project team' 
    elsif interested_in_granting 
    'in assessing this project for a grant' 
    elsif interested_in_investing 
    # elsif eoi.invest || @invest 
    'in investing in the outcomes of this project' 
    else 
    nil 
    end 
end 

如果他们使用@eoi实例,其余方法也是如此。

+0

我试过这个,但然后我得到一个错误时我尝试呈现索引,其中指出:错误的参数数量(给定0,预期1) – Mel

+0

当我尝试使用表单时(也使用该辅助方法),它也会出现错误 – Mel

+0

您必须提供参数正在调用接受非可选参数的方法。请检查更新后的答案。 – Andolasoft

相关问题