我在显示我的模型时遇到问题
I am having problems with my views displaying my model
在此应用中,客户可以有多个项目,项目只能有一个客户。
当我尝试访问 projects/new:
时,我一直收到此错误
ActionController::ParameterMissing in ProjectsController#new
缺少参数或值为空:项目
这是在我的项目控制器中突出显示的行:
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'
这里的问题是@client
没有价值。
假设您将在 new
操作
中收到 params[:client_id]
我认为您需要将代码更改为 link:
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
更新:我设法解决了这个问题,因为我正在通过不同的模型创建模型的实例,当我转到 projects/new 时 link 不应该工作。但话又说回来,非常感谢 Gokul M 和你们其他人试图帮助我。
在此应用中,客户可以有多个项目,项目只能有一个客户。
当我尝试访问 projects/new:
时,我一直收到此错误ActionController::ParameterMissing in ProjectsController#new
缺少参数或值为空:项目
这是在我的项目控制器中突出显示的行:
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'
这里的问题是@client
没有价值。
假设您将在 new
操作
params[:client_id]
我认为您需要将代码更改为 link:
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
更新:我设法解决了这个问题,因为我正在通过不同的模型创建模型的实例,当我转到 projects/new 时 link 不应该工作。但话又说回来,非常感谢 Gokul M 和你们其他人试图帮助我。