【发布时间】:2016-02-19 17:21:24
【问题描述】:
我有一个名为 user 的设计模型。
我还有一个名为项目的模型。
我希望用户在创建用户帐户时创建一个项目。我的功能正常工作;但是,用户项目没有保存到数据库中。
这里是 projects_controller.rb
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!
# GET /projects
# GET /projects.json
def index
@projects = current_user.projects
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)
@project.user = current_user
respond_to do |format|
if @project.save
format.html { redirect_to root_path, notice: 'Thanks! Your project has been saved. Please check your email inbox now.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
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 @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
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 projects_url, notice: 'Project was successfully destroyed.' }
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(:service, :location, :details, :email)
end
end
和用户.rb
class User < ActiveRecord::Base
has_many :projects, :dependent => :destroy
accepts_nested_attributes_for :projects, :reject_if => lambda { |a| a[:content].blank}, allow_destroy: true
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :lockable, :timeoutable
end
和项目模型
class Project < ActiveRecord::Base
belongs_to :user
end
以及创建用户/他们的第一个帖子的表单
= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |u|
= devise_error_messages!
= u.fields_for :project do |p|
.field = p.text_field :service, placeholder: "What service do you need?"
.field = p.text_field :location, placeholder: "What's the project location?"
.field = u.email_field :email, autofocus: true, placeholder: "What's your email address?"
.actions = u.submit "Click to continue"
关于如何让我的项目索引视图仅显示属于当前用户的帖子有什么想法吗?
【问题讨论】:
-
您尝试创建用户帐户的部分在哪里?帖子控制器与您的问题有什么关系,您提到的是用户、用户帐户和项目而不是帖子?
-
错字 - 帖子应该是项目。
标签: ruby-on-rails ruby devise