1

,客戶端可能有許多項目和項目只能有一個客戶端。我在我的視圖中遇到問題,在此應用程序中顯示我的模型

我不斷收到這個錯誤,當我嘗試訪問項目/新:

的ActionController :: ParameterMissing在ProjectsController#新

參數是丟失或爲空值:項目

這是在我的項目控制器中突出顯示的行:

params.require(:project).permit(:client_id,:project_description,:project_timescale)

這是我的客戶端模式:

class Client < ActiveRecord::Base 
has_many :projects, dependent: :destroy 
validates :name, presence: true 
end 

這是我的項目模型:

class Project < ActiveRecord::Base 
belongs_to :client 
validates :project_description, :client, presence: true 
end 

這些都爲客戶我的移民

class CreateClients < ActiveRecord::Migration 
def change 
create_table :clients do |t| 
    t.string :name, presence: true, null: false 

    t.timestamps null: false 
end 
end 
end 

的項目遷移:

class CreateProjects < ActiveRecord::Migration 
def change 
create_table :projects do |t| 
    t.belongs_to :client, index: true, foreign_key: true, null: false 
    t.text :project_description, null: false, presence: true 
    t.string :project_timescale 

    t.timestamps null: false 
end 
end 
end 

控制器爲客戶端:

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

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

# GET /clients/1 
# GET /clients/1.json 
def show 
end 

# GET /clients/new 
def new 
@client = Client.new 
end 

# GET /clients/1/edit 
def edit 
end 

# POST /clients 
# POST /clients.json 
def create 
@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 
    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(:name) 
    end 
    end 

控制器的項目:

class ProjectsController < ApplicationController 
    before_action :set_project, only: [:new, :create] 

    # GET /projects 
    # GET /projects.json 
    def index 
    @projects = Project.all 
    end 

    # GET /projects/1 
    # GET /projects/1.json 
    def show 
    end 

    # GET /projects/new 
    def new 
    @project = @client.projects.new 
    end 

    # GET /projects/1/edit 
    def edit 
    end 

    # POST /projects 
    # POST /projects.json 
    def create 
    @project = @client.projects.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 
    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 = Client.find_by(id: params[:client_id]) || Client.find(project_params[:client_id]) 
    end 

    # Never trust parameters from the scary internet, only allow the white list through. 
    def project_params 
     params.require(:project).permit(:client_id, :project_description, :project_timescale) 
    end 
end 

這是用於項目視圖(new.html.haml)

%h1 New project 

= render 'form' 

= link_to 'Back', 

這是表格代碼,仍然用於項目(form.html.haml)

= form_for @project do |f| 
    - if @project.errors.any? 
    #error_explanation 
     %h2= "#{pluralize(@project.errors.count, "error")} prohibited this project from being saved:" 
     %ul 
     - @project.errors.full_messages.each do |msg| 
      %li= msg 

    .field 
    = f.label :client 
    = f.text_field :client 
    .field 
    = f.label :project_description 
    = f.text_area :project_description 
    .field 
    = f.label :project_timescale 
    = f.text_field :project_timescale 
    .actions 
    = f.submit 'Save' 
+0

發表您的看法(html.erb)代碼。 –

+0

我剛更新了代碼看看。 – Tito

+0

如果只是讓它變新並結束,註釋掉變量賦值,會發​​生什麼? –

回答

0

更新:我設法解決這個問題,因爲我創建一個模型的實例通過一個不同的模型,當我去項目/新鏈接是不應該工作。但是再次,非常感謝Gokul M和你們其他人試圖幫助我。

0

這裏的問題在@client中沒有任何價值。 假設你將在new行動

我想你需要更改代碼鏈路接收params[:client_id]此:

before_action :set_client, only: [:new, :create] 

... 
# GET /projects/new 
def new 
    @project = @client.projects.new 
end 
... 

private 
def set_client 
    @client = Client.find_by(id: params[:client_id]) || Client.find(project_params[:client_id]) 
end 
+0

我試過了,它仍然沒有工作,當我嘗試查看項目/新 – Tito

+0

我打算爲項目的唯一屬性是客戶端ID作爲外鍵 – Tito

+0

是你在調用'/ projects/new'時傳遞'client_id'作爲參數?如果不是,則需要在爲客戶端創建項目時傳遞它。 –

相關問題