2017-03-17 7 views
0

私は新しいですStackoverflow &レール、こんにちは!レールは "に属して" "たくさん"と結果を格納するには

私は周りを見てきたし、一緒にすべてを得ることはできません。

私は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 
関連する問題