У меня есть проект, к которому я добавляю сеанс, и для сеанса проекта я пытаюсь добавить задачу. Я могу создать проект и добавить проектную сессию для проекта, но когда я пытаюсь добавить задачу для сеанса, используя project_sessions_id
, я получаю ошибку Couldn't find ProjectSession with 'id'=
и 'app/controllers/tasks_controller.rb:60:in set_project_session
Я также могу получить идентификатор сеанса проекта project_sessions/11
в URL-адресе, но когда я нажимаю 'create task' Я получаю эту ошибку. как я могу это решить?Не удалось найти ProjectSession с 'id' =
вот что я сделал
ProjectSessionController:
class ProjectSessionsController < ApplicationController
before_action :set_project_session, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_project
def index
@project_sessions = ProjectSession.all
end
def show
@project_sessions = ProjectSession.where(project_id: @project.id).order("created_at DESC")
end
def new
@project_session = ProjectSession.new
end
def edit
end
def create
@project_session = ProjectSession.new(project_session_params)
@project_session.project_id = @project.id
#respond_to do |format|
if @project_session.save
redirect_to @project
#format.html { redirect_to @project_session, notice: 'Project session was successfully created.' }
#format.json { render :show, status: :created, location: @project_session }
else
format.html { render :new }
format.json { render json: @project_session.errors, status: :unprocessable_entity }
end
#end
end
def update
respond_to do |format|
if @project_session.update(project_session_params)
format.html { redirect_to @project_session, notice: 'Project session was successfully updated.' }
format.json { render :show, status: :ok, location: @project_session }
else
format.html { render :edit }
format.json { render json: @project_session.errors, status: :unprocessable_entity }
end
end
end
def destroy
@project_session.destroy
respond_to do |format|
format.html { redirect_to project_sessions_url, notice: 'Project session was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project_session
@project_session = ProjectSession.find(params[:id])
end
def set_project
@project = Project.find(params[:project_id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_session_params
params.require(:project_session).permit(:session_date, :session_name, :session_description, :start_time, :end_time)
end
end
контроллер Задача:
class TasksController < ApplicationController
before_action :set_task, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
before_action :set_project_session
def index
@tasks = Task.all
end
def show
end
def new
@task = Task.new
end
def edit
end
def create
@task = Task.new(task_params)
@task.session_id = @project_session.id
respond_to do |format|
if @task.save
redirect_to @project_session
else
format.html { render :new }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @task.update(task_params)
format.html { redirect_to @task, notice: 'Task was successfully updated.' }
format.json { render :show, status: :ok, location: @task }
else
format.html { render :edit }
format.json { render json: @task.errors, status: :unprocessable_entity }
end
end
end
def destroy
@task.destroy
respond_to do |format|
format.html { redirect_to tasks_url, notice: 'Task was successfully destroyed.' }
format.json { head :no_content }
end
end
private
def set_task
@task = Task.find(params[:id])
end
def set_project_session
@project_session = ProjectSession.find(params[:project_session_id])
end
def task_params
params.require(:task).permit(:name, :description)
end
end
маршруты:
Rails.application.routes.draw do
get 'hr_dashboard/index'
resources :roles
resources :project_sessions
devise_for :users
resources :tasks
resources :projects do
resources :project_sessions, except: [:show, :index]
end
resources :project_sessions do
resources :tasks, except: [:show, :index]
end
authenticated :user do
root 'admindashboard#index', as:"authenticated_root"
end
root 'welcome#index'
get 'userdashboard/index'
get 'admindashboard/index'
get 'welcome/index'
end
Просмотр для создания новой задачи
<div class="container">
<h1>New Task</h1>
<%= form_for(@task) do |f| %>
<% if @task.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@task.errors.count, "error") %> prohibited this task from being saved:</h2>
<ul>
<% @task.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br>
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= link_to 'Back', tasks_path %>
</div>
Можете ли вы разместить свои маршруты –
@ j-dexx я обновил сообщение с помощью маршрутов .. –
Итак, ваши маршруты вложены, можете ли вы предоставить код вида для ссылки, пожалуйста. –