'will_paginate undefined method' in views but not controller - Sinatra 2.0, ActiveRecord 5.0 - sinatra

I added the will_paginate gem as outlined in the gem installation
for Sinatra. I never get a 'no method error' inside the controller, but in the routed view I keep getting a 'no method for will_paginate #'. Answers related to Rails (link1, link2) are not fixing this. This mongoid related question came close.
Here's an outline of the code used:
In controller app:
require 'will_paginate'
require 'will_paginate/active_record'
class SinatraApp < Sinatra::Base
get '/post' do
#post = Post.all.paginate(:page => params[:page], :per_page => 30)
erb :index
end
end
In erb index view:
<%= will_paginate #posts %>
In Gemfile:
gem 'will_paginate', '~> 3.1.0'
Like I said, it only breaks in the view and not in the controller which leads me to believe it is a Sinatra specific case.

My solution was this:
In controller app
class Post < Sinatra::Base
configure do
register WillPaginate::Sinatra
end
end
This is definitely a Sinatra::Helper problem because this mongoid specific answer also works. Choose your poison.
My understanding here is that the WillPaginate module injects itself directly into Sinatra's Helpers module (a monkey patch). In certain environments, like mine, you have to extend this into Sinatra by registering it. Hope this saves someone some time.

Related

TYPO3 Hook not found

I am currently trying to write a Hook to add extra fields to a Flexform. Therefore I followed this tutorial: https://docs.typo3.org/typo3cms/extensions/news/DeveloperManual/ExtendNews/ExtendFlexforms/Index.html?fref=gc&dti=250938618364487#extend-flexforms-with-custom-fields
But when I go to a page in the backend that contains an options from a Flexform I get the following Error:
Class 'ID\SearchBarAdditional\Hooks\FlexFormHook' not found.
I register the Hook in the ext_localconf like this:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'][\TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools::class]['flexParsing'][] = \ID\SearchBarAdditional\Hooks\FlexFormHook::class;
and my Hook-file is here: typo3conf/ext/search_bar_additional/Classes/Hooks
and is initialized that way:
namespace ID\SearchBarAdditional\Hooks;
class FlexFormHook { /* ... */
So in my opinion everything is in the right place and should work, but I do still get the error that TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance("ID\SearchBarAdditional\Hooks\FlexFormHook") fails.
Do you guys have any ideas, what could be wrong? Do I have to register the Hook in \TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin( or something similar (as the posted code is really the only thing I've done)?
This is a typical class loading error. Check that you added your PHP namespaces to composer autoloading and/or ext_emconf.php and make sure your filenames are correctly named according to PSR-4. If in doubt you can inspect the class loading map files generated by composer in vendor/composer (if you use composer for class loading, which you definitely should do).
I found the mistake: My Hook does indeed not get loaded. I tried to 'include' it in the ext_localconf.php and it is working now.
But as this is of course an extremely ugly solution I posted a second question, how to load a hook here: Typo3 8.X - autoload Hook
Thank you for your help!!!

How to use Router Connect method correctly in CakePHP 2 and 3?

I have a controller MyUsersController with an action login.
I have mapped this with:
$routes->connect('/member/login', [ 'controller' => 'MyUsers', 'action' => 'login', 'plugin'=>false, 'prefix'=>FALSE]);
Now I can access this action "login" using these two urls which is not good for SEO.
http://localhost/cakephpapp/member/login
http://localhost/cakephpapp/my-users/login
So, how can I disable the second URL?
I have tried this code which is working fine but I don't know whether this is correct method or not.
$routes->redirect('/my-users/login', '/member/login');
There is a line in the default routes file that provides the fallbacks so that you can access pages using controller/action. This is really to help you quickly scaffold an app. In CakePHP 3 this is:-
$routes->fallbacks('InflectedRoute');
As commented in the file you can/should remove this line once you have set up the routes for your application. If you remove this it will also prevent your error logs from filling with errors like 'Missing Controller' which can be an issue on a production site, particularly if it is replacing an existing one.
In CakePHP 2 the equivalent line is this:-
require CAKE . 'Config' . DS . 'routes.php';
Removing these lines does mean that you will have to define all routes for your application, but that is probably a better practice than relying on automagic routes of Cake (or any framework). There's a good article on this by Phil Sturgeon: Beware the Route to Evil.

Capistrano custom strategy without making a gem

Is there a way of making a custom deploy strategy for capistrano, but without having to bundle it as a gem? I've searched and searched but everyone's examples are packaged as gems.
I just want to stick it in the include path and require it in the Capfile.
Something like this should work:
require 'capistrano/recipes/deploy/strategy/remote_cache'
class InsaneStrategy < Capistrano::Deploy::Strategy::RemoteCache
def copy_repository_cache
do_something()
end
end
set :strategy, InsaneStrategy.new(self)

How do I make a separate View path/directory for iPhone in Rails?

I'm making an iPhone version of an existing Rails app. I'd like to make the mobile version accessible via a subdomain such as iphone.mysite.com.
I know I can use formats and the respond_to block for individual erb files, such as index.iphone.erb as show here:
Creating an iPhone optimised version of your Rails site using iUI and Rails 2
But I'd like to keep entirely separate view directories for the mobile version and regular version such as this:
app/views/iphone
Here's what I've tried in my Application controller:
class ApplicationController < ActionController::Base
before_filter :set_site
def set_site
subdomain=self.request.subdomains[0]
ActionController::Base.prepend_view_path("app/views/#{subdomain}")
end
When testing this, however, the view switches to the view associated with the last requested subdomain by any user.
For example, if I visit http://iphone.mysite.com, then immediately go to http://www.mysite.com in another separate browser, I see the mobile version instead of the regular one. Refreshing it will correct this and bring up the right version. But if I go back to http://iphone.mysite.com in the other browser and refresh, it brings up the non-mobile site! I'm tearing my hair out and not understanding what's going on.
Any advice would be much appreciated.
Edit 1
Vlad below found a link with a possible solution however it is not working for me. Here is the code I tried. I made a file called subdomain_view.rb and placed it in config/initializers:
# Put all of this in a bootstrap-only initializer
ActionController::Base.class_eval do
APP_ONE_VIEW_PATH = "app/views/iphone"
APP_TWO_VIEW_PATH = "app/views/default"
cattr_accessor :application_view_path
self.view_paths = ["app/views", APP_ONE_VIEW_PATH, APP_TWO_VIEW_PATH]
# This is where you determine the switching mechanism for your application. Here, it is a simple GET parameter.
# You can probably argue that this specific piece SHOULD be in your actual app_controller class definition, as it is the only piece
# of info pertinent to the rest of your application.
before_filter do |controller|
ActionController::Base.application_view_path = request.subdomains[0]=="iphone" ? APP_TWO_VIEW_PATH : APP_ONE_VIEW_PATH
end
end
require 'aquarium'
ActionView::PathSet.class_eval do
include Aquarium::DSL
before :find_template do |join_point, object, *args|
object.each_with_index do |path,i|
object.unshift(object.delete_at(i)) if path.to_s == ActionController::Base.application_view_path
end
end
end
# I'll leave the exercise of testing this or implementing it for your particular app up to you.
With the above code, I am getting the same view no matter what subdomain I put in. Any suggestions on what might be wrong? Am I putting this code in the wrong place?
First, you have an error in your approach. You only 'set' a view path, you don't 'unset' it. When you do something like
ActionController::Base.prepend_view_path
this actually persists between your requests (internally, you are setting a class variable, and because classes are cached in production, this variable remains set between requests). Therefore, the current view path is the view path of your last request.
IMO, you should dynamically compute view_path for your current subdomain (this implies some ActionView hacking). A nice solution is provided here.
I was able to solve the problem by using a gem called themes_for_rails:
https://github.com/lucasefe/themes_for_rails
After installing the gem, here's what I added to my application files:
#application_controller.rb
class ApplicationController < ActionController::Base
theme :theme_resolver
def theme_resolver
current_subdomain=self.request.subdomains[0]
end
end
#routes.rb
MyAppName::Application.routes.draw do
themes_for_rails
end
#Gemfile
gem 'themes_for_rails'
I placed my themes in [application_root]/themes. Make sure you don't put it in [application_root]/app/themes.

Wordpress And Zend

I am wanting to do stuff with the Google Data API, the contacts specifically. The easist method i have found so far is using Zend. The problem I am having is adding the Zend framework. Does anyone know how to do this with WordPress?
Thanks
you can use "hardcoded" includes if you fail to setup autoloading ->
in /wp-content/themes/levitation/send.php insert to the first line:
require_once 'your/path/to/zend/Zend/GData/ClientLogin.php';
Problem is you need to get through all the errors and alwas include the missing class (inside classes are the includes taken care of...
Or in the main file (guess index.php) insert:
set_include_path(get_include_path() . PATH_SEPARATOR . 'your/path/to/zend/');
//for ZF below 1.8
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
//for ZF > 1.8
require_once 'Zend/Loader/Autoloader.php';
Zend_Loader_Autoloader::setFallbackAutoloader(true);
It should be pretty trivial.
Write and test some bootstrap code that sets up ZF's autoloading and make sure it generally works.
Stick that code in a wordpress plugin, and tie things up to the right hooks in wordpress.
Try this http://blueberryware.net/2008/09/04/wp-library-autoloader-plugin
I think all you need is there.
Make sure that you are calling:
Zend_Loader::loadClass('Zend_Gdata_ClientLogin');
before you are using that class. And that you have a developer key.