Created
January 15, 2018 06:09
-
-
Save basicavisual/a6d8c1dd9956ae52664411f9bb0be8e6 to your computer and use it in GitHub Desktop.
can't pass instance variables from controller to view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TasksController < ApplicationController | |
before_action :set_project | |
before_action :set_task, except: [:index, :new, :create] | |
def index | |
@tasks = @project.tasks.all | |
@completed = @tasks.completed | |
end | |
private | |
def set_task | |
@task = @project.tasks.find(params[:id]) | |
end | |
def set_project | |
@project = Project.find(params[:project_id]) | |
end | |
def task_params | |
params.require(:task).permit(:name, :deadline, :project_id) | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Task < ApplicationRecord | |
belongs_to :project | |
has_many :comments, dependent: :destroy | |
scope :completed, -> { where.not( completed: nil ) } | |
scope :priority, -> { where( priority: true ) } | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% @completed.each do |completed| %> | |
// this fails with undefined method `each' for nil:NilClass | |
hello <%= completed.name %> | |
<% end %> | |
<% @project.tasks.each do |completed| %> | |
// however this doesn't fail | |
hello <%= completed.name %> | |
<% end %> | |
<% @tasks.each do |task| %> | |
<p> | |
<% if task.completed %> | |
// this goes alright | |
<% else %> | |
// this too | |
<% end %> | |
</p> | |
<% end %> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment