ProjectSession在這裏我有一個項目,我正在添加一個會議,我正在嘗試添加任務的項目會話。 我能夠創建項目,並添加項目會話的項目,但是當我試圖添加任務會話使用project_sessions_id
我得到錯誤Couldn't find ProjectSession with 'id'=
和'app/controllers/tasks_controller.rb:60:in set_project_session
我能夠得到項目會話ID也project_sessions/11
在url中,但當我點擊'創建任務'我得到這個錯誤。我該如何解決這個問題?找不到與'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我已更新與路線的帖子.. –
好的,所以你的路線是嵌套的,你可以提供鏈接的視圖代碼請。 –