Reputation: 6659
Rails 3.2
In my controllers/tickets_controller.rb, I have:
def update
@ticket = Ticket.find(params[:id])
@ticket.save_customer_info_if_present(params, @ticket)
respond_to do |format|
if @ticket.update_attributes(params[:lead_billing])
format.html { redirect_to @ticket, notice: 'Ticket was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @ticket.errors, status: :unprocessable_entity }
end
end
end
And in my models/ticket.rb, I have:
def self.save_customer_info_if_present(params, ticket)
customer_info_params = params[:customer_info]
if !customer_info_params.nil?
customer_info = CustomerInfo.new(
:ticket_id => ticket.id,
:first => customer_info_params["first"],
:last => customer_info_params["last"],
:status => 'entered'
)
customer_info.save
end
end
When the update action is run, I get an "method not found: save_customer_info_if_present"
Why isn't the controller finding the self method defined in the model?
Upvotes: 0
Views: 1087
Reputation: 44380
You call a method on the instance of the model:
@ticket.save_customer_info_if_present(params, @ticket)
But define this method as the class method:
def self.save_customer_info_if_present(params, ticket)
# some code here
Remove the self
keyword from the method definition, also you can remove a ticket argument because you already call this method on the ticket instance(make it more readable):
def save_customer_info_if_present(params)
if params[:customer_info].present?
customer_info = CustomerInfo.new(
:ticket_id => id, # just an id because it in the ticket instance
:first => params[:customer_info]["first"],
:last => params[:customer_info]["last"],
:status => 'entered'
)
customer_info.save
end
end
Class and Instance Methods in Ruby
Upvotes: 2