STI wth Strong params undefined local variable or method `attributes' - ruby-on-rails

undefined local variable or method `attributes' for #CarsController:0x00007fa686991b30
def cars_params(type)
params.require(type.to_sym).permit(attributes)
end
My model is CommonCar::RedTrunk, so type has become "common_car_red_trunk"
I am using STI and following this.. https://gist.github.com/danielpuglisi/3c679531672a76cb9a91
I don't really understand why attributes was used and why its now failing. I assumed it maybe took the attributes from the required model, but not sure. Any insights would be helpful to get this working.

Related

Active Model Serializer issue for Nil object

ActiveModelSerializers::SerializableResource.new(object.project, include: [collaborator: :pictures]).as_json[:project]
When object.project is nil then it occurs an issue.
NoMethodError: undefined method `[]' for nil:NilClass
It's not related to ActiveModelSerializers (AMS) after all. In Ruby, nil causes a lot of NoMethodError, so we often avoid nils with if or unless modifiers.
For example, in your case, we often do
ActiveModelSerializers::SerializableResource.new(object.project, include: [collaborator: :pictures]).as_json[:project] if object.project

Rails NoMethodError: undefined method `method_name' for #<Object>

I am getting undefined method 'xyz' for #. 'xyz' is a instance method written inside ABC Class. Calling the method using delayed job although the object exists. Why I am getting this error. Please anyone help me.
Code Snippet:
device_obj.delay(run_at: 5.minutes.from_now).get_device_battery_status
In Device model:
def get_device_battery_status # Used in delayed Job
command_data = {"mode"=>"get_battery"}
self.send_command(command_data)
end

Strange undefined method model_name issue

I want to edit my record in the database with nested attributes. In my controller I have:
def edit
#chocolate = Chocolate.new.kinds.find_by_id(params[:chocolate_id])
end
and in my edit.html.erb I have:
form_for #chocolate do |choco|
but it gives me the next error:
undefined method model_name issue
I cannot understand why it gives me such error. Any options?
Those methods aren't really made to be mixed together in that way.
I'm guessing that you're trying to do something like this instead:
#chocolate = Chocolate.includes(:kinds).find(params[:chocolate_id])
Then you are querying the database for the Chocolate and associated Kind records instead of trying to instantiate a new record.

undefined method `attribute_method_matcher' for nil:NilClass

I'm getting this error "undefined method `attribute_method_matcher' for nil:NilClass".
My controller name is Cad Its function is
def index
#cadempty = Cad.new
#caddata = Cad.all
end
The error is on creating the new object. If I comment Cad.new the code works fine.
Earlier I thought it could be because I have a method named 'new' and I was Using User.new to create a blank object for the form. But its not the error I renamed the method to something else and the error still exists. I have no idea what I'm doing wrong.
Maybe one of your column names in the database table is a reserved word.
Avoid using names for methods that are reserved words in the language.

Rails 3 undefined method `create' for nil:NilClass error while trying create a related object

I have two models, User and Profile, in one-to-one relation and I am trying to create a new profile for a user if it does not exist yet:
user = User.includes(:profile).find( params[:user_id] )
unless user.profile.present?
user.profile.create
end
But I am getting an error: undefined method `create' for nil:NilClass
Well, two things. Firstly, I assume the code is wrong, as that only enters the block if the profile is there (and hence can't create it).
if user.profile.blank?
user.profile.create
end
looks like more correct code.
Secondly, when you use a has_one, you don't use .create like you do with has_many. This is because the relation object is directly returned, and not a "proxy" method like a has_many. The equivalent method is create_profile (or create_x where x is the object)
Hence, try the following code:
if user.profile.blank?
user.create_profile
end

Resources