I want to update attributes of a model:
def update
MyModel.update_attributes params[:id], params[:mymodel]
#.....
end
But it says undefined method `update_attributes' for #<Class:0x0000000396ecb0>. I wonder, isn't this the same as https://stackoverflow.com/a/840323/1708058
update_attributes is an instance method of ActiveRecord::Relation, you have to use the class method update:
MyModel.update(params[:id], params[:mymodel])
To use update_attributes you could do:
#my_model = MyModel.find(params[:id])
#my_model.update_attributes(params[:mymodel])
Related
I am getting this error:
undefined method `posts' for nil:NilClass
I have a model Post, here is my PostsController
def new
#post = current_user.posts.build
end
def create
#user = current_user
#post = #user.posts.build(post_params)
end
The current_user field in your code is not getting set, and has a value of nil. Hence, when you try to access current_user.posts, it translates to fetching posts for a nil class object. As there is no such method for objects of nilclass, we get an error.
I have set a mailer:
def created_poll_email(poll)
#poll = poll
mail(subject: 'A new poll was created! ' + #poll.question)
end
Firstly I was calling it on polls controller:
def create
#poll = Poll.new(poll_params.merge(user: current_user))
if #poll.save
PollMailer.created_poll_email(#poll).deliver_now
And it was working fine.
Now I want to move it to a callback on model:
after_save :send_email
def send_email
PollMailer.created_poll_email(#poll).deliver_now
end
But now i get error undefined method `question' for nil:NilClass. I tried to set also other callbacks as after_create or after_commit but same result.
Why is this happening and how can I fix?
You should replace #poll, which isn't set inside your model, with self:
def send_email
PollMailer.created_poll_email(self).deliver_now
end
class MyAwesomeClass
def foobar
puts "trip!"
end
So that I can perform :
MyAwesomeClass.foobar
=> "trip!"
I keep getting :
NoMethodError: undefined method `foobar' for MyAwesomeClass:Class
class MyAwesomeClass
def self.foobar
puts "trip!"
end
end
Using "self" makes the method a class instance method
I have defined a problems method in my Report model. I need to use the value of Report.problem in the report's controller while defining the action show. But i keep getting the error message 'undefined method problem'. How do i solve this? Any assistance would be greatful.
I have a report model and a problem model that contains a list of all problems.
In report model
def problems1
Problem.find(:all, :conditions => )
end
In the reports controller i need something like
def show
#report = Report.problems1
end
you have to assign self.method_name to use as a class method
Follow following rule for Model methods
Class Method
def self.problem
end
in controller
Report.problem
Instance method
def problem
end
in controller
report = Report.new
report.problem
If you define method as class method
class Report < ActiveRecord :: Base
def Report.problem
puts 1
end
end
Report.problem
>1
But if you define method as object
class Report < ActiveRecord :: Base
def problem
puts 1
end
end
This method call
report = Report.new
report.problem
>1
I've been trying to do SEO friendly urls, and managed to get it work, but when I call index action on blogs, I get a weird "undefined method `parameterize' for nil:NilClass." The method works when using show method.
#model
def to_s
title
end
def to_param
"#{id}-#{to_s.parameterize}"
end
#controller
#blogs = Blog.find.all
Screenshot of error
http://www.freeimagehosting.net/image.php?83e76a260b.png
Turns out you can't call title.parameterize on to_param without error. So I added a permalink column and called parameterize on that.
#models/blog.rb
before_save :permalink
def to_param
"#{id}-#{permalink}"
end
def permalink
self.permalink = self.title.parameterize
end
And voila. I knew it was something really stupid.
In case anyone is having trouble with this in Rails 5....I left out the #to_s part, which is critical.
class Post < ApplicationRecord
def to_param
slug
end
def slug
"#{id}-#{pretty_url}"
end
def pretty_url
title.to_s.parameterize
end
end