Skip to content

Instantly share code, notes, and snippets.

@basicavisual
Created January 15, 2018 06:09
Show Gist options
  • Save basicavisual/a6d8c1dd9956ae52664411f9bb0be8e6 to your computer and use it in GitHub Desktop.
Save basicavisual/a6d8c1dd9956ae52664411f9bb0be8e6 to your computer and use it in GitHub Desktop.
can't pass instance variables from controller to view
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
class Task < ApplicationRecord
belongs_to :project
has_many :comments, dependent: :destroy
scope :completed, -> { where.not( completed: nil ) }
scope :priority, -> { where( priority: true ) }
end
<% @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