I'm upgrading an app from rails 3.something to rails 5. For some reason, I'm am getting undefindied method respond_to anytime I use that method in any of my controllers. This was working before and I was hoping someone here could help.
class StatusController < ApplicationController
#this throws an error 'undefined method respond_to
respond_to :json, :html
end
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.
Related
Someone help me to mount grape framework...
require 'grape'
class Api < Grape::API
format :json
resources :articles do
get do
{hello: 'hello world!'}
end
end
end
Also i wanna to change custom routes...
I have a Sinatra Ruby app with the ActionMailer gem for sending emails. The email sending functionality works fine, but I can't figure out how to use the preview functionality for development. My mailer mailer.rb is located in lib/companyname/mailers, and my preview mailer_preview.rb is located in spec/companyname/mailers/previews. When I run my app and navigate to http://localhost:26250/rails/mailers I get a 404 "Sinatra doesn't know this ditty" page.
What do I need to do to be able to see the previews in my browser?
mailer.rb
module CompanyName
class Mailer < ActionMailer::Base
def test_email(recipient_email_address)
email = mail(to: recipient_email_address, from: "no-reply#companyname.com", subject: "Testing ActionMailer") do |format|
format.html { "<h1>Testing</h1>" }
end
email.deliver_now
end
end
end
mailer_preview.rb
module CompanyName
class MailerPreview < ActionMailer::Preview
def test_email
Mailer.test_email("test#email.com")
end
end
end
After looking at the code for ActionMailer I couldn't find any non-Rails method of configuration, so it looks like this isn't currently possible*. I ended up setting up a Sinatra endpoint to load the HTML from the MailerPreview class and just display it in the browser:
mailers_controller.rb
get "/emails/:email_name" do
halt 404 unless ENV["ENABLE_MAIL_TEST_ENDPOINT"] == "true"
email_preview = CompanyName::MailerPreview.public_send(params["email_name"])
email_preview.html_part.decoded # Note: I switched to the Mail gem instead of ActionMailer, so the code may not be identical
end
*I am not a Rails expert so this may not be the case, however to the best of my abilities I could not see a solution.
I have a rails application which is api only i am using gem rails-api. Now I am trying to create admin panel on it because we realised later that we do need an admin panel. But i think rails-api doesnt have good support for views.
When I try to submit forms it says undefined method protect_against_forgery? maybe because it doesnt supports <%= csrf_meta_tags %>
I tried to define manually this method in my controller but no luck.
def protect_against_forgery?
true
end
How can i submit the forms without getting this error.
in your api controller put following line.
skip_before_filter :verify_authenticity_token
or try with
def protect_against_forgery?
false
end
this may be help you.
According to rails-api, try to set config.api_only = false to config/application.rb file
Or, since protect_from_forgery is a method that belongs to ActionController, try to add require "action_controller/railtie" to application.rb
I have an issue when i am loging in to site after clicking on other menu tabs it is automatically loging out and if i wait for few minutes refresh and then login tabs are working fine.If i logout and login again and i click on other tabs geting loged out. I am not understanding where i need to check the code.
class SessionsController < Devise::SessionsController
# POST /resource/sign_in
def create
respond_to do |format|
format.json do
self.resource = warden.authenticate!(auth_options)
sign_in(resource_name, resource)
return render :json => resource
end
format.html do
super
end
end
end
end
I think issue it related to your authenticity_token
If you make a links in menu with the help of Rails form helper then You will not face this issue
I changed the default in controller
protect_from_forgery with: :exception
to
protect_from_forgery with: :null_session
will also solve problem in rails 4
here is my controller:
class AdminController < ApplicationController
before_filter :require_user
authorize_resource :class => false
def index
end
def users_list
end
end
here is my Ability class:
class Ability
include CanCan::Ability
def initialize(user)
if user.admin?
can :manage, :all
else
can :read, :all
end
end
end
when trying to access "/admin/users_list" (with an admin user or without) i get the following error:
uninitialized constant CanCan::Rule::Mongoid
any thoughts?
Just recently, CanCan added support for Mongoid and renamed CanDefinition to Rule, so the error you are getting indicates you are using the latest CanCan code from the git repo.
Try out CanCan version 1.4 from rubygems and see if that solves the problem. There may be some bug fixes needed before 1.5 is released to rubygems.
UPDATE:
This bug was fixed in CanCan version 1.5.0.beta1.