简体   繁体   English

参数未传递给控制器

[英]Params not passing to controller

Im trying to pass a client_id parameter from one of my views to a different models controller, but it seems like the param isn't being passed since I get the error "undefined method `client_id=' for nil:NilClass" in the set_client method. 我试图将一个client_id参数从我的一个视图传递到另一个模型控制器,但是似乎没有传递参数,因为我在set_client方法中收到错误“ nil:NilClass的未定义方法'client_id ='” 。 What am I doing wrong? 我究竟做错了什么? ANy help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

<%= link_to 'New assessment', new_assessment_path(:client_id => @client.id), class: "btn btn-xs btn-success" %>

This is my controller: 这是我的控制器:

class AssessmentsController < ApplicationController
  before_action :set_assessment, only: [:show, :edit, :update, :destroy]
  before_action :set_client, only: [:new]

  def new
    @assessment = Assessment.new
  end



 def create
    @assessment = Assessment.new(assessment_params)
    respond_to do |format|
      if @assessment.save
        format.html { redirect_to @assessment, notice: 'Assessment was successfully created.' }
        format.json { render :show, status: :created, location: @assessment }
      else
        format.html { render :new }
        format.json { render json: @assessment.errors, status: :unprocessable_entity }
      end
    end
  end



  def set_client
    @assessment.client_id = params[:client_id]
  end

Routes: 路线:

Rails.application.routes.draw do

resources :trainers
resources :clients
resources :assessments
root 'pages#home'

Main Reason for the undefined error is You are passing client_id to new method but not to the create method. 未定义错误的主要原因是您将client_id传递给新方法,而不传递给create方法。 And What i understood from ur question is You want add a assessment to a client. 我从您的问题中了解到的是,您想向客户添加评估。

If this is your goal follow these steps 如果这是您的目标,请执行以下步骤

1.create association in respective models. 1.在各个模型中创建关联。

add the following line in Client.rb 在Client.rb中添加以下行

has_many: :assesments

and add in Assement.rb 并添加Assement.rb

belongs_to: Client

2.Now change ur routes.rb. 2.现在更改您的routes.rb。

resources clients do
  resources assements do
  end
end 

3.Now check ur routes by executing this cmd rake routes now your routes should be like this 3.现在通过执行此cmd 耙路检查您的路线,现在您的路线应该像这样

/clients/:client_id/assements/new(.:format)

4.Now you dont need to pass the client id param manually. 4.现在,您无需手动传递客户端ID参数。

Hope this will help you in solving ur problem. 希望这会帮助您解决您的问题。

cmt here if any.. 如果有,请在这里

Your error reads: 您的错误显示为:

undefined method `client_id=' for nil:NilClass nil:NilClass的未定义方法`client_id ='

That's coming from: 来自:

def set_client
  @assessment.client_id = params[:client_id]
end

Which is being called before your new action due to: 由于以下原因,在您的new操作之前调用了哪个方法:

before_action :set_client, only: [:new]

At that point, @assessment is nil in set_client because you haven't yet assigned the variable. 此时,在set_client @assessment为nil,因为您尚未分配变量。 Thus the error. 因此错误。

It has nothing to do with params not passing. 它与不传递的参数无关。

My suggestion is, remove the before_action and change the new action to: 我的建议是,删除before_action并将new操作更改为:

def new
  @assessment = Assessment.new(client_id: params[:client_id])
end

set_client方法中执行Assessment.find_by(client_id: params[:client_id])

You are trying to set the client_id in @assessment , and that variable is not defined when you are trying to set it. 您正在尝试在@assessment设置client_id,并且在尝试设置该变量时未定义该变量。

before_action 's callback is executed, as the name says, before the action (AKA method). before_action的回调在动作之前执行(AKA方法)。

You should remove the before_action and add the setter within your new method 您应该删除before_action并在新方法中添加setter

def new
  @assessment = Assessment.new
  @assessment.client_id = params[:client_id]
end
  • 1.Set client in before_action 1.将客户端设置为before_action
  • 2.no need to create @assesment in new method 2.无需在新方法中创建@assesment

Change as below 更改如下

class AssessmentsController < ApplicationController
  before_action :set_assessment, only: [:show, :edit, :update, :destroy]
  before_action :set_client, only: [:new]

  def new
  end



 def create
    @assessment = Assessment.new(assessment_params)
    respond_to do |format|
      if @assessment.save
        format.html { redirect_to @assessment, notice: 'Assessment was successfully created.' }
        format.json { render :show, status: :created, location: @assessment }
      else
        format.html { render :new }
        format.json { render json: @assessment.errors, status: :unprocessable_entity }
      end
    end
  end



  def set_client
    @client_id = Client.find_by(params[:client_id])
  end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM