将"belongs to"添加到"has many"并存储结果的边栏



我是Stackoverflow和rails的新手,但你好!

我环顾四周,无法将所有内容放在一起。

我正在创建一个仪表板,允许用户添加具有 4 个不同字段的新客户端。然后允许用户将具有 8 个不同字段的项目添加到特定客户端。但是,我正在努力将项目添加到客户端并使其显示。我已经在以下位置公开了我的代码:https://github.com/JackStovell/revenue-dashboard。

请将我的代码撕成碎片并告诉我哪里出错了,以便我可以调整它!

谢谢

以下是一些片段:

项目型号:

class Project < ActiveRecord::Base
belongs_to :client
end

客户端模型

class Client < ActiveRecord::Base
has_many :projects
end

项目控制器

class ProjectsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_project, only: [:show, :edit, :update, :destroy]

# GET /projects
# GET /projects.json
def index
@projects = Project.all
@clientnav = Client.all
end
# GET /projects/1
# GET /projects/1.json
def show
@project = Project.find(params[:id])
@clientnav = Client.all
end

# GET /projects/new
def new
@project = Project.new
@client = Client.all
@clientnav = Client.all
end
# GET /projects/1/edit
def edit
@clientnav = Client.all
@client = Client.all
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
@clientnav = Client.all
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
@clientnav = Client.all
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
@clientnav = Client.all
end
def income
@project.income = params[@project.billing] - params[@project.cost]
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(:id, :name, :jobNumber, :status, :billing, :cost, :income)
end

end

客户端控制器

class ClientsController < ApplicationController
skip_before_action :verify_authenticity_token
before_action :set_client, only: [:show, :edit, :update, :destroy]


# GET /clients
# GET /clients.json
def index
@clients = Client.all
@clientnav = Client.all
end

# GET /clients/1
# GET /clients/1.json
def show
@client = Client.find(params[:id])
@projects = @client.projects
@clientnav = Client.all
end
# GET /clients/new
def new
@client = Client.new
@clientnav = Client.all
end
# GET /clients/1/edit
def edit
@clientnav = Client.all
end
# POST /clients
# POST /clients.json
def create
@clientnav = Client.all
@client = Client.new(client_params)
respond_to do |format|
if @client.save
format.html { redirect_to @client, notice: 'Client was successfully created.' }
format.json { render :show, status: :created, location: @client }
else
format.html { render :new }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /clients/1
# PATCH/PUT /clients/1.json
def update
respond_to do |format|
if @client.update(client_params)
format.html { redirect_to @client, notice: 'Client was successfully updated.' }
format.json { render :show, status: :ok, location: @client }
else
format.html { render :edit }
format.json { render json: @client.errors, status: :unprocessable_entity }
end
end
@clientnav = Client.all
end
# DELETE /clients/1
# DELETE /clients/1.json
def destroy
@client.destroy
respond_to do |format|
format.html { redirect_to clients_url, notice: 'Client was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_client
@client = Client.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def client_params
params.require(:client).permit(:clientName, :clientOwner, :analysis, :analysis2)
end
end

代码:

clients_controller.rb

def new
@client = Client.new
@project = @client.projects.build
end
def create
@client = Client.new(client_params)
@client.save
end
def client_params
params.require(:client).permit(:id, :client_name, ...,
project_attributes: [:name, ...] )
end

项目/_form.html.erb

<%= f.label :clientName %>
<%= f.text_field :clientName"%>
....
....
<%= f.fields_for :projects do |project| %>
<%= project.label :name %>
<%= project.text_field :name%>
....
....
<%end%>

model/client.rb

accepts_nested_attributes_for :projects

最新更新