Auto_link with rails_autolink gem - undefined method - ruby-on-rails

I'm trying to use the rails_autolink gem.
As usual, I added the gem declaration to my gemfile:
gem 'rails_autolink'
Then ran "bundle install", restarted my rails local server and
Then I added to my posts_controller the require before the class declaration
require 'rails_autolink'
class PostsController < ApplicationController
...
and used the auto_link method inside my create action
def create
#new_post = Post.new(params[:post])
if #new_post.content == ""
redirect_to posts_url
else
#new_post.content = auto_link(#new_post.content)
... #respond_to and save methods
end
end
end #end of my post controller
The thing is that when I try to create a post I have a undefined method on the auto_link method, any idea why ? Is it specific to the gem or is it because something else ?

This is a view helper which you're trying to use in a controller
#new_post.content = view_context.auto_link(#new_post.content)

In any other places you can use: ActionController::Base.helpers.auto_link(text)

I don't believe you need the namespacing. Try it again simply using:
auto_link(#new_post.content)

Related

Making Jbuilder work with Rails 5 API Mode

I want to use Jbuilder with Rails 5.0.0.beta1.1 in API mode. Out of the box, it doesn't work, even when creating the app/views directory.
For example, I have:
# app/controllers/tests_controller.rb
class TestsController < ApplicationController
# The requests gets inside the action
def test
end
end
# app/views/tests/test.json.jbuilder
json.test "It works!"
The error I'm getting is
No template found for TestsController#test, rendering head :no_content
I guess I have to change some things in the config files. What do I have to do?
Doing an explicit render from the controller like this works:
render 'controller_name/action.json.jbuilder'
With API mode.
You need include module like bellow
class ApplicationController < ActionController::API
include ActionController::ImplicitRender # if you need render .jbuilder
include ActionView::Layouts # if you need layout for .jbuilder
end
I got the same error, but in my case I had simply forgotten to add the jbuilder gem in the Gemfile:
gem 'jbuilder', '~> 2.5'

Rails using ActionView::Helper methods outside of view

I've created a helper which I'd like to use for text manipulation
module ApplicationHelper
module TextHelper
extend ActionView::Helpers
end
end
However when I run ApplicationHelper::TextHelper.simple_format "foo" in Rails console I get
NoMethodError: undefined method `white_list_sanitizer' for Module:Class
Is there anything else I need included?
I have already looked at https://github.com/rails/rails/issues/13837 but it didn't work.
Using Rails 4, Ruby 1.9.3
If you're in the console, you should be able to just do helper.simple_format('hi'). The helper method is available in console as a way to call some helper methods.
When using a custom helper:
# app/helpers/custom_helper.rb
module CustomHelper
def custom_method(x)
puts "Custom method #{x}"
end
end
# from the console
helper.custom_method('hi')
# from the controller
class SomeController < ApplicationController
def index
view_context.custom_method('hi')
end
end

How to have a dynamic redirect in rails?

how do I add a dynamic redirect? I tried it in ApplicationController and ApplicationHelper with no success.
I want something like this:
def dynamic_path
if current_user.admin?
admin_path
else
overview_path
end
end
What's the best practice for that?
Thanks!
Edit:
Oh, I forgot to mention that I want to put this into my gem which is used by two different applications and both should use this method. Where exactly should I put it?
Try putting this in the ApplicationController and then add a helper_method line at the top of the application controller like so:
helper_method :dynamic_path
def dynamic_path
redirect_to (current_user.admin? ? admin_path : overview_path)
end
The helper_method line makes this method available in all your views and controllers.
Use redirect_to:
def dynamic_path
redirect_to (current_user.admin? ? admin_path : overview_path)
end
Update: And since it sounds like you're trying to store this helper module in an external gem you'll need to ensure your module gets loaded as an ActionView helper, which can be done automatically by using a Railtie in your gem. See
How do I extract Rails view helpers into a gem?

Declarative Auth blocking instance variables

I'm using declarative authorization in my rails app, and I have the following controller:
class OrganizationsController < ApplicationController
filter_resource_access attribute_check: true
def index
#organizations = Organization.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: #organizations }
end
end
end
However when I try to access the view for that action, I get the error
undefined method 'each' for nil:NilClass on the line where I try to access the #organizations instance variable. It works when I comment out the filter_resource_access line.
Any thoughts?
the error is because your instance variable is Nil, is why it says undefined method each for nil:NilClass. filter is a method that runs either before or after the controller, in this case i'm not exactly sure what that method means but its not letting you access the data until after the controller is my guess.
Apparently I forgot to include the declarative_authorization gem in my Gemfile. :shame:
All is working now.

How can one override a class method inside a module inside a gem?

I'm trying to add a rescue statement to an otherwise failing gem. The trouble is is that I'm not correctly overriding the original gem's method. How can I accomplish this?
Original Gem
module OmniAuth
class Configuration
include Singleton
def add_camelization(name, camelized)
self.camelizations[name.to_s] = camelized.to_s
end
initializers/omniauth.rb
module OmniAuth
class Configuration
def add_camelization(name, camelized)
begin
self.camelizations[name.to_s] = camelized.to_s
rescue
puts "No camelization for #{camelized}"
end
# ^ This rescue statement is not being called to replace the original gem's method.
end
end
end
Try
OmniAuth::Configuration.class_eval do
def add_camelization(name, camelized)
...
end
end

Resources