Reputation: 2551
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. 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'
Upvotes: 0
Views: 995
Reputation: 93
Main Reason for the undefined error is You are passing client_id to new method but not to the create method. 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.
add the following line in Client.rb
has_many: :assesments
and add in Assement.rb
belongs_to: Client
2.Now change ur 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
/clients/:client_id/assements/new(.:format)
4.Now you dont need to pass the client id param manually.
Hope this will help you in solving ur problem.
cmt here if any..
Upvotes: 2
Reputation: 2675
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
Upvotes: 1
Reputation: 20253
Your error reads:
undefined method `client_id=' for nil:NilClass
That's coming from:
def set_client
@assessment.client_id = params[:client_id]
end
Which is being called before your new
action due to:
before_action :set_client, only: [:new]
At that point, @assessment
is nil in set_client
because you haven't yet assigned the variable. 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:
def new
@assessment = Assessment.new(client_id: params[:client_id])
end
Upvotes: 1
Reputation: 494
You are trying to set the client_id in @assessment
, and that variable is not defined when you are trying to set it.
before_action
's callback is executed, as the name says, before the action (AKA method).
You should remove the before_action
and add the setter within your new method
def new
@assessment = Assessment.new
@assessment.client_id = params[:client_id]
end
Upvotes: 1
Reputation: 101
Do Assessment.find_by(client_id: params[:client_id])
in your set_client
method.
Upvotes: 1