点击#new&时如何停止NoMethodError #编辑? [英] How to stop NoMethodError when clicking #new & #edit?
问题描述
我很难弄清楚如何将多个控制器的逻辑合并到边栏中.
例如,我想在侧边栏中有一个使用habitus_controller中的@habits
逻辑的部分,以及一个使用goals_controller中的@unaccomplished_goals
逻辑的部分.
对于这个问题,我们只关注@unaccomplished_goals
.
<% @top_3_goals.each do |goal| %>
正确显示在应用程序的侧边栏中,如果我单击链接@top_3_goals = @unaccomplished_goals.top_3 BUT 派生的rel ="nofollow">目标/新或目标/1/编辑我很害怕:undefined method each for nil:NilClass
!
以下是视图细分:
我在views/layouts/application.html.erb
中使用<%= render 'sidebar/sidebar' %>
抓住views/sidebar/_sidebar.html.erb
以便使用<%= render 'goals/upcoming' %>
抓住views/goals/_upcoming.html.erb
视图/目标/_upcoming.html.erb
<table>
<% @top_3_goals.each do |goal| %>
<tr>
<td>
<strong>
<%= link_to goal.name, edit_goal_path(goal) %>
</strong>
</td>
<td>
<%= goal.deadline.strftime("%m-%d-%Y") %>
</td>
</tr>
<% end %>
</table>
views/sidebar/_sidebar.html.erb
<div id="sidebarsection" class="panel panel-default">
<div id="sidebarheading" class="panel-heading"><h5><b>Upcoming</b></h5></div>
<%= render 'goals/upcoming' %>
</div>
目标控制者
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@goals = Goal.all.order("deadline")
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
@top_3_goals = @unaccomplished_goals.top_3
end
end
目标模型
class Goal < ActiveRecord::Base
belongs_to :user
acts_as_taggable
scope :accomplished, -> { where(accomplished: true) }
scope :unaccomplished, -> { where(accomplished: false) }
validates :name, presence: true
scope :top_3, -> do
order("deadline DESC").
limit(3)
end
end
我是否需要在sidebar_controller中放入任何内容?我在那里添加了逻辑,但似乎没有任何区别.我仍然是一个初学者,所以我在这里所说的可能没有任何意义.现在,在sidebar_controller中我什么也没有,因为我从那里看到的唯一视图是_sidebar.html.erb
(我不太确定partials如何利用控制器).如果在目标索引中渲染了部分_upcoming.html.erb
,它会完美工作(所有链接均正常工作),但是如果在_sidebar中进行渲染,如何使它工作?
非常感谢您对我的支持=]
除非有计划使侧栏具有某种iframe或其他功能(这很奇怪),否则不应将其作为侧栏控制器. /p>
就像@patrickmcgraw建议的那样,我将在应用程序控制器中创建一个before_action以确保始终设置这些变量.
class ApplicationController < ActionController::Base
before_action :set_top_3_goals
def set_top_3_goals
@top_3_goals = current_user.goals.unaccomplished.top_3
end
end
I'm having a hard time figuring out how to incorporate multiple controller's logic into a sidebar.
For example, I want to have a section in the sidebar that uses @habits
logic from the habits_controller and a section that uses @unaccomplished_goals
logic from the goals_controller.
For this question let's just focus on @unaccomplished_goals
.
<% @top_3_goals.each do |goal| %>
shows correctly in the app's sidebar, which is derived from the goals_controller #index @top_3_goals = @unaccomplished_goals.top_3
BUT if I click on links goals/new or goals/1/edit I get the dreaded: undefined method each for nil:NilClass
!
Here's a view breakdown:
I use <%= render 'sidebar/sidebar' %>
in views/layouts/application.html.erb
to grab views/sidebar/_sidebar.html.erb
in order to use <%= render 'goals/upcoming' %>
to grab views/goals/_upcoming.html.erb
views/goals/_upcoming.html.erb
<table>
<% @top_3_goals.each do |goal| %>
<tr>
<td>
<strong>
<%= link_to goal.name, edit_goal_path(goal) %>
</strong>
</td>
<td>
<%= goal.deadline.strftime("%m-%d-%Y") %>
</td>
</tr>
<% end %>
</table>
views/sidebar/_sidebar.html.erb
<div id="sidebarsection" class="panel panel-default">
<div id="sidebarheading" class="panel-heading"><h5><b>Upcoming</b></h5></div>
<%= render 'goals/upcoming' %>
</div>
goals controller
def index
if params[:tag]
@goals = Goal.tagged_with(params[:tag])
else
@goals = Goal.all.order("deadline")
@accomplished_goals = current_user.goals.accomplished
@unaccomplished_goals = current_user.goals.unaccomplished
@top_3_goals = @unaccomplished_goals.top_3
end
end
goal model
class Goal < ActiveRecord::Base
belongs_to :user
acts_as_taggable
scope :accomplished, -> { where(accomplished: true) }
scope :unaccomplished, -> { where(accomplished: false) }
validates :name, presence: true
scope :top_3, -> do
order("deadline DESC").
limit(3)
end
end
Do I need to put anything in the sidebar_controller? I played around with adding logic there but it didn't seem to make any difference. I'm still a beginner so what I'm saying here may make no sense. Right now I have nothing in the sidebar_controller since the only view I have from there is _sidebar.html.erb
(I'm not quite sure how partials utilize controllers). The partial _upcoming.html.erb
worked perfectly if I rendered it in the goals index (the links all worked), but how can I get it to work if I render it in the _sidebar?
Thanks for putting up with me this far =]
There shouldn't be such a thing as a sidebar controller, unless you plan to make the sidebar some sort of iframe or something ( which is strange ).
Like @patrickmcgraw suggested, I would create a before_action in the application controller to make sure that those variables are always set.
class ApplicationController < ActionController::Base
before_action :set_top_3_goals
def set_top_3_goals
@top_3_goals = current_user.goals.unaccomplished.top_3
end
end
这篇关于点击#new&时如何停止NoMethodError #编辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!