Undefined Method Respond_To in Rails 5 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.

Rails 5 Integration test fails with NoMethodError: undefined method `[]=' for nil:NilClass when using Devise helper sign_in

Looks like I found the issue. Apparently, in rails-api mode, the ActionDispatch::Cookies and ActionDispatch::Session::CookieStore middlewares are inserted in the end of the middleware stack, which doesn't occur in normal Rails mode.

Due to this, those middlewares are included after Warden::Manager which messes up something in request specs.

Try to set in test.rb

Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Cookies
Rails.application.config.middleware.insert_before Warden::Manager, ActionDispatch::Session::CookieStore

Rails 5 – Can't update single attribute with link_to, undefined method `to_sym'

The scope attribute on the validation is expecting either a single attribute or an array of attributes from your model, something like

class Product < ApplicationRecord
validates_uniqueness_of :code, :scope => :archived
end

As it turns out, including the :scope option adds a where clause during the validation which looks like where(:archived => false) based on my example above.

From your original code validates_uniquesness_of :code, :scope => { :archived => false }, this results in the scope executing where({:archived => false} => nil). You can see that the hash argument to where is definitely off.

Later in the stack, ActiveRecord is going to try to turn the key from the argument hash into a symbol with to_sym and look for that key as an attribute on the Product model. However, the key in this case is the hash {:archived=>false} which does not respond to to_sym, and here the undefined method error gets raised.

The reason the error "appeared" in the controller is because the line product.update(product_params) in your controller is the beginning of the call chain that led to the bad validation being called in the model, which you have determined was the source of the error.



Related Topics



Leave a reply



Submit