Reputation: 139
So I'm learning rails via writing some simple application and in that app I have two types of objects: Tasks and Projects.Sense is that all those objects can be created by user,user must be able to manipulate them and so on.Each task belongs_to
project and project has_many
tasks.But when I create new project - it already has all tasks from all another projects,so it looks like:
This '123' task was created by simply adding task to 'Project' but same task still appear in 'Another project'.How can I fix this bug and make my task unique?I think something must be added to task or projects model but I don't know what should I add.
Here are corresponding models and controllers:
Task.rb
class Task < ActiveRecord::Base
belongs_to :project
end
Project.rb
class Project < ActiveRecord::Base
belongs_to :user
has_many :tasks, dependent: :destroy
validates :name, presence: true, uniqueness: true
end
tasks_controller.rb
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
# GET /tasks
# GET /tasks.json
def index
@tasks = Task.all
end
# GET /tasks/1
# GET /tasks/1.json
def show
end
# GET /tasks/new
def new
@task = Task.new
end
# GET /tasks/1/edit
def edit
end
# POST /tasks
# POST /tasks.json
def create
@task = Task.new(task_params)
respond_to do |format|
if @task.save
format.html { redirect_to home_url }
format.json { render :show, status: :created, location: @task }
else
format.html { render :home_url }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /tasks/1
# PATCH/PUT /tasks/1.json
def update
respond_to do |format|
if @task.update(task_params)
format.html { redirect_to home_url }
format.json { render :home_url, status: :ok, location: @task }
else
format.html { render :home_url }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
# DELETE /tasks/1
# DELETE /tasks/1.json
def destroy
@task.destroy
respond_to do |format|
format.html { redirect_to home_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
@task = Task.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def task_params
params.require(:task).permit(:deadline, :name)
end
end
projects_controller.rb
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
# GET /projects
# GET /projects.json
def index
@projects = Project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to home_url }
format.json { render :show, status: :created, location: @project }
else
format.html { render :home_url }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to home_url }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :home_url }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to home_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name)
end
end
task view -
<% @tasks.each do |task| %>
<div class="row task">
<div class="col-xs-12">
<div class="col-xs-1 checkbox">
<%= check_box_tag 'accept' %>
</div>
<div class="col-xs-8 taskbody">
<%= task.name %>
</div>
<div class="mini-glyph">
<div class="col-xs-1">
<span class="glyphicon glyphicon-arrow-up"></span>
<span class="glyphicon glyphicon-arrow-down"></span>
</div>
<div class="col-xs-1">
<%= link_to edit_task_path(task) do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
</div>
<div class="col-xs-1">
<span><%= link_to " ", task, method: :delete, data: { confirm: 'Are you sure?' }, class:"glyphicon glyphicon-trash" %></span>
</div>
</div>
</div>
</div>
<% end %>
Upvotes: 0
Views: 375
Reputation: 4453
To set the project_id when you create a task, add something like this to your form:
<%= f.select :project_id, options_for_select(Project.choices) %>
then in project.rb
def self.choices
all_projects = []
Project.find_each do |project|
# show the name but save the id
all_projects << [project.name, project.id]
end
all_projects
end
Then change display the tasks with Task.where(project_id: project_id)
instead of Task.all
as the first two comments suggest.
Upvotes: 1