Undefined Instance Method "Respond_To" in Rails 5 API Controller

Undefined instance method respond_to in Rails 5 API Controller

ActionController::API does not include the ActionController::MimeResponds module. If you want to use respond_to you need to include MimeResponds.

class ApplicationController < ActionController::API
include ActionController::MimeResponds
end


module Api
class MyController < ApplicationController
def method1
# ...
respond_to do |format|
format.xml { render(xml: "fdsfds") }
format.json { render(json: "fdsfdsfd" ) }
end
end
end
end

Source: ActionController::API docs

undefined method respond_to in Rails 5 controller

As per Rails 5 release notes,

Remove respond_to/respond_with placeholder methods, this functionality
has been extracted to the responders gem.

Add responders gem to your Gemfile.

Undefined method `respond_to' for Api::V1::Controller

In your orders controller, you made a spelling mistake. Change

repsond_with Order.all

to

respond_with Order.all

Also, you made a spelling mistake in your routes file too. Change:

namespace :api, defaults: { format: 'josn' } do

to

namespace :api, defaults: { format: 'json' } do

Undefined method when calling model method in controller

EDIT:

My first answer was before you mentioned Devise and assuming you didn't know if you needed a class or instance method. It's clear that your method must be an instance one.

I think you are trying to apply something you found here: https://stackoverflow.com/a/30857087/3372172

This requires that you add a new field to the users database, to manage the :login_bypass_token. Because you will use this column later to perform a find_by. Devise does not add this column to the database.

PREVIOUS ANSWER

If the method needs to access instance variables (which means it acts differently depending the specific object in the User class), it should be an instance method, defined without the self keyword.

If it is a class method, it cannot depend on any attribute from a specific object, and you cannot call it from an instance of the class.

You must decide if it's really a class method or an instance method.
`
If you need a class method to be called from an instance, you can do this (but I don't know why you could need it).

class User
def self.method_name
# blablabla
end

def method_name
User.method_name
end
end

undefined method in controller action new

You have fallen victim to to the evil non-breaking space character or one of its cousins such as the hair space.

While visibly identical the Ruby parser does not treat the non-breaking space character U+00A0 the same as the normal U+0020 character. Ruby instead treats it as an identifier. Which is why you get undefined method ` '.

Turn on the hidden characters in your editor and go hunting for those pesky NBSP's.

undefined method `includes' for object instance

includes can only be called on ActiveRecord::Relations or ActiveRecord::Base children classes, account seems to be a instace of a model, thus not respond to ActiveRecord::Relation methods like includes. It seems a false positive from bullet as soon as you are only getting account from current_user, although I don't recommend delagations to associations, it can lead to N + 1 problems in the feature.


Replacing delagate with AR association:

Assuming that Account is a regular ActiveRecord model you can use has_one macro (look at documentation for more details):

has_one :account, through: :role

It's very handful when you need to iterate through users:

# You can avoid `N + 1`
User.includes(:account).limit(50).each { |u| puts u.name }

Rails 5: unable to retrieve hash values from parameter

take a look to this. Very weird since ActionController::Parameters is a subclass of Hash, you can convert it directly to a hash using the to_h method on the params hash.

However to_h only will work with whitelisted params, so you can do something like:

permitted = params.require(:line_item).permit(: line_item_attributes_attributes)
attributes = permitted.to_h || {}
attributes.values

But if instead you do not want to whitelist then you just need to use the to_unsafe_h method.

Update

I was very curious about this issue, so I started researching, and now that you clarified that you are using Rails 5, well that's the cause of this issue, as @tillmo said in stable releases of Rails like 4.x, ActionController::Parameters is a subclass of Hash, so it should indeed respond to the values method, however in Rails 5 ActionController::Parameters now returns an Object instead of a Hash

Note: this doesn’t affect accessing the keys in the params hash like params[:id]. You can view the Pull Request that implemented this change.

To access the parameters in the object you can add to_h to the parameters:

params.to_h

If we look at the to_h method in ActionController::Parameters we can see it checks if the parameters are permitted before converting them to a hash.

# actionpack/lib/action_controller/metal/strong_parameters.rb
def to_h
if permitted?
@parameters.to_h
else
slice(*self.class.always_permitted_parameters).permit!.to_h
end
end

for example:

def do_something_with_params
params.slice(:param_1, :param_2)
end

Which would return:

{ :param_1 => "a", :param_2 => "2" }

But now that will return an ActionController::Parameters object.

Calling to_h on this would return an empty hash because param_1 and param_2 aren’t permitted.

To get access to the params from ActionController::Parameters, you need to first permit the params and then call to_h on the object

def do_something_with_params
params.permit([:param_1, :param_2]).to_h
end

The above would return a hash with the params you just permitted, but if you do not want to permit the params and want to skip that step there is another way using to_unsafe_hash method:

def do_something_with_params
params.to_unsafe_h.slice(:param_1, :param_2)
end

There is a way of always permit the params from a configuration from application.rb, if you want to always allow certain parameters you can set a configuration option. Note: this will return the hash with string keys, not symbol keys.

#controller and action are parameters that are always permitter by default, but you need to add it in this config.
config.always_permitted_parameters = %w( controller action param_1 param_2)

Now you can access the params like:

def do_something_with_params
params.slice("param_1", "param_2").to_h
end

Note that now the keys are strings and not symbols.

Hope this helps you to understand the root of your issue.

Source: eileen.codes



Related Topics



Leave a reply



Submit