首先,我为代码量和我要问的内容道歉。但我拼命地需要帮助。我无论如何都无法理解这个概念。
我有一个基本的CRUD(项目),我试图将另一个CRUD(讨论)嵌套到项目中,以便可以对每个项目进行讨论。现在,我一直试图连续五天这样做。我无法理解我的生活。我已经阅读并研究了所有要阅读和研究的内容。我无法自己解决这个问题。
所以,我开始新鲜了。我已经建立了一个新项目并获得了基础知识,但我不知道从哪里开始。如果有人能花时间给我一步一步的指示,我会非常感激。或者,如果你能够快速完成,甚至可能为我完成我的代码?因为我将不得不做大约5个以上,所以如果我有1个完全完成的我可以参考那将是如此惊人。
projects_controller.rb
class ProjectsController < ApplicationController
def index
@projects = Project.all
end
def show
end
def new
@projects = Project.new
end
def create #no view
@projects = Project.new(project_params)
if @projects.save
redirect_to projects_path, :notice => "Your project was sent!"
else
render "new"
end
end
def edit
@projects = Project.find(params[:id])
end
def update #no view
@projects = Project.find(params[:id])
if @projects.update_attributes(project_params)
redirect_to projects_path, :notice => "Your project has been updated."
else
render "edit"
end
end
def destroy #no view
@projects = Project.find(params[:id])
@projects.destroy
redirect_to projects_path, :notice => "Your project has been deleted."
end
private
def project_params
params.require(:project).permit(:title, :description)
end
end
discussions_controller.rb
class DiscussionsController < ApplicationController
def index
@discussion = Discussion.all
end
def show
@discussions = Discussion.find(params[:id])
@projects = @discussions.Project.all
end
def new
@discussions = Discussion.new
end
def create #no view
@discussions = Discussion.new(discussion_params)
if @discussions.save
redirect_to discussions_path, :notice => "Your discussion was submitted."
else
render "new"
end
end
def edit
@discussions = Discussion.find(params[:id])
end
def update #no view
@discussions = Discussion.find(params[:id])
if @discussions.update_attributes(discussion_params)
redirect_to discussions_path, :notice => "Your discussion has been updated."
else
render "edit"
end
end
def destroy #no view
@discussions = Discussion.find(params[:id])
@discussions.destroy
redirect_to discussions_path, :notice => "Your discussions has been deleted."
end
private
def discussion_params
params.require(:discussion).permit(:title, :description)
end
end
的routes.rb
Rails.application.routes.draw do
resources :homes
resources :projects
resources :discussions
root "homes#index"
型号:
discussion.rb
class Discussion < ActiveRecord::Base
belongs_to :project
end
project.rb
class Project < ActiveRecord::Base
has_many :discussions
end
迁移:
_create_projects.rb
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :title
t.text :description
t.date :due_date
t.timestamps null: false
end
end
end
_create_discussions.rb
class CreateDiscussions < ActiveRecord::Migration
def change
create_table :discussions do |t|
t.string :title
t.text :description
t.timestamps null: false
end
end
end
_create_nested_discussions
class NestedDiscussion < ActiveRecord::Migration
def change
add_column :discussions, :project_id, :integer
end
end
答案 0 :(得分:0)
我注意到的一件事是您不允许在强参数中使用project_id属性。因此,在discussion_controller.rb中的参数中添加:project_id
:
private
def discussion_params
params.require(:discussion).permit(:title, :description, :project_id)
end
答案 1 :(得分:0)
您可能希望您的路线如此:
resources :projects do
resources :discussions
end