2017-03-17 23 views
0

我是新來的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 

回答

0

代碼:

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%> 

模型/ client.rb

accepts_nested_attributes_for :projects