2017-01-20 99 views
0

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> 
+0

你可以張貼你的路線 –

+0

@ j-dexx我已更新與路線的帖子.. –

+0

好的,所以你的路線是嵌套的,你可以提供鏈接的視圖代碼請。 –

回答

1

在任務控制你的行動創造,你必須添加:

@task= @project_session.tasks.build(task_params) 

因爲現在,你不告訴的任務,從project_session建立(或對於project_session)你只是告訴它創建一個新的任務。

@task = Task.new 

而在routes.rb中的文件,你已經準備通過嵌套的資源的情況發生,所以它目前正在尋找一個任務belongs_toproject_session的ID但找不到任何。

resources :tasks 
: - 我想你可以說 - 因爲你仍然可以有未嵌套資源創建ProjectSssion當你已經讓這種形式知道它應該屬於哪條路線到

而且也是,在你form_for元素

如果沒有父母的情況下project_sessions不能創建,那麼沒有理由保留它,所以你應該刪除它。

不管怎麼說,這裏的的form_for應該是什麼樣子:

<%= form_for([@project_session, @task]) do |f| %> 
+0

任務屬於一個項目會話,您已經解釋瞭如果項目會話屬於某個任務該怎麼做。 –

+0

我更新了我的答案以供將來參考。感謝您指出了這一點。 – Crashtor

1

我想通了!

我忘了在

<%= form_for([@project_session, @task]) do |f| %> 

添加@project_session剛添加的,它的工作。