Sinatra and "controllers" behaviour - sinatra

Sinatra from Box does't allow separate action to file? Like this:
index.php
get '/' and other
user.php
get '/user/show/'
post '/user/new/' and other
How to say sinatra use user.php for '/user/*' request, and index.php for '/'.
And how looks application with many get post in one file written in sinatra? (one huge ass?)

After reading a lot, exist some solution:
1.
class Get < Sinatra::Base
get('/') { 'GET!' }
end
class Post < Sinatra::Base
post('/') { 'POST!' }
end
class Routes < Sinatra::Base
get('/') { Get.call(env) }
post('/') { Post.call(env) }
end
run Routes
2.
class Foo < Sinatra::Base
get('/foo') { 'foo' }
end
class Bar < Sinatra::Base
get('/bar') { 'bar' }
end
Routes = Rack::Mount::RouteSet.new do |set|
set.add_route Foo, :path_info => %r{^/foo$}
set.add_route Bar, :path_info => %r{^/bar$}
end
run Routes

Related

Codeigniter 404 Page Not Found in route with two folders

How do I solve this one in Codeigniter? I am new in MVC framework Codeigniter.
I have three folders in application/views:
pages
dashboard.php
members.php
Starter
login.php
register.php
templates
header.php
footer.php
If I put this code in route:
$route['(:any)'] = 'pages/view/$1';
I couldn't run my login http://localhost/login - it says 404 Page not found,
but if I remove the route, it worked!
But my http://localhost/members - will redirect on the http://localhost/dashboard if I remove the route:
$route['(:any)'] = 'pages/view/$1';
And here are my controllers:
Pages.php
class Pages extends CI_Controller {
function view( $page = 'dashboard')
{
$this->load->helper('url');
if( ! file_exists('application/views/pages/'.$page.'.php'))
{
show_404();
}
$this->load->view('templates/header');
$this->load->view('pages/'.$page);
$this->load->view('templates/footer');
}
}
Starter.php
class Starter extends CI_Controller {
function view( $page = 'login')
{
$this->load->helper('url');
if( ! file_exists('application/views/starter/'.$page.'.php'))
{
show_404();
}
$this->load->view('starter/'.$page);
}
}
How can I fix the issues?
Please help :(

Mojolicious: determine controller based on placeholder parameters

I know that it's possible to write code like this.
$r->post('/v1/:controller/:action')
And mojo will call appropriate controller class.
Is it possible to do something like this in Mojo (maybe with stash):
$r->post('/v1/:model/:method')->to(
action => ':method',
controller => sub {
my ($captures) = #_;
my $controller = $captures->{'model'};
...
return $controller;
}
)

Rails 4 welcome wizard, how to correct this code to make it work for rails 4?

Try to build a welcome wizard and try to get existing rails code to be ported to be rails 4 compatible. Based mostly on previous great answer: https://stackoverflow.com/a/17255451/355281
I try to call http://books:3000/welcome/basics
This results in:
Circular dependency detected while autoloading constant WelcomeController
app/controllers/welcome_controller.rb
class Welcome::ApplicationController < ::ApplicationController
layout "welcome"
before_filter :authentice_user!
end
app/controllers/welcome_basics_controller.rb
class Welcome::BasicsControlller < Welcome::ApplicationController
before_action :authenticate_user!
before_filter :allowed?
def new
#step = Welcome::Basics.new(current_user)
end
def create
#step = Welcome::Basics.new(current_user)
if #step.save(params[:welcome_basics])
redirect_to welcome_some_other_step_path, :notice => "Yay"
else
render :new
end
end
private
def step
#step ||= Welcome::Basics.new(current_user)
end
helper_method :step
def allowed?
redirect_to previous_step_path unless step.allowed?
end
end
**app/models/welcome_basics.rb
class Welcome::Basics
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
def persisted?
false
end
def initialize(user)
#user = user
end
attr_reader :user
delegate :some_field, :some_other_field, :to => :user
validates_presence_of :some_field
def save(params)
user.some_field = params[:some_field]
user.some_other_field = params[:some_other_field]
if valid?
user.step = 2
user.save
end
end
def photo
#photo ||= Photo.new
end
def profile
#profile ||= user.profiles.first
end
end
/config/routes.rb
namespace :welcome do
resource :basics, :only => [:new, :create]
end
I see a few things that appear to be off. The one that appears to be the biggest issue with Rails 4 vs Rails 3.2 is in your pseudo-model definition.
class Welcome::Basics
include ActiveModel::Model # Necessary for Rails 4
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
...
end
In your welcome_controller.rb, you have authentice user which should read authenticate user.
In your welcome_basics_controller.rb you misspelled controller in your definition, currently: Welcome::BasicsControlller should be Welcome::BasicsController.
I'd also recommend that you change your naming and inheritance to be a bit simpler and more inline with Rails convention over configuration. This would be my approach:
Since welcome_basics.rb is a pseudo-model, I'd put it in its own directory to prevent any potential automatic sourcing of ActiveModel. I'd also change the name to the singular to be in line with Rails conventions for models.
app/pseudo_models/welcome_basic.rb
class WelcomeBasic
include ActiveModel::Model # Necessary for Rails 4
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
...
end
Now I'd put your welcome_controller in a sub-directory called "welcome" within the controller directory:
app/controllers/welcome/welcome_controller.rb
class Welcome::WelcomeController < ApplicationController
layout "welcome"
before_filter :authenticate_user!
end
Then your basics controller should be called basics within the "welcome" directory, and since your inheriting from your welcome controller, you don't need to repeat the authenticate_user! line. Also remember that you'll need to rename your calls to your pseudo-model from Welcome::Basics to WelcomeBasic. For Rails 4 you'll also need to implement strong params in your controller.
app/controllers/welcome/basics_controller.rb
class Welcome::BasicsController < Welcome::WelcomeController
before_filter :allowed?
def new
#step = WelcomeBasic.new(current_user)
end
def create
#step = WelcomeBasic.new(current_user)
if #step.save(basic_params) #updated to use strong_params
redirect_to welcome_some_other_step_path, :notice => "Yay"
else
render :new
end
end
private
#For strong_params
def basic_params
params.require(:welcome_basic).permit(:attribute1, :attribute2)
end
def step
#step ||= WelcomeBasic.new(current_user)
end
helper_method :step
def allowed?
redirect_to previous_step_path unless step.allowed?
end
end
/config/routes.rb
...
namespace :welcome do
resources :basics, :only => [:new, :create]
end
...
The other thing that you'll need to ensure is that your corresponding views are placed within the same directory structure as your controller, so you should have the following:
/app/views/welcome/basics/welcome.html.erb
One other note, when you say "I try to call http://books:3000/welcome/basics", I assume you mean that your attempting to post your form? Otherwise, you should be calling http://books:3000/welcome/basics/new in order to get your basics form. If you want that route to map to http://books:3000/welcome/basics you'll need to make the corresponding adjustments in your config/routes.rb file.

Forwarding a request with custom routes in zend framework

I am working with custom routes in zend framework. I had managed my custom routes successfully
but I had a problem i.e. I want to forward a request according some predefined condition after routing but I am unable to forward my. I did not get any error.
Following is the code of routing configuration file -
;--- routing for multiple requests
routes.resource.route = "resource/:moduleName/:fileType/"
routes.resource.defaults.controller = resources
routes.resource.defaults.module = 'default'
routes.resource.defaults.action = 'index'
;--- routing for singles file
routes.resourceCSS.type = "Zend_Controller_Router_Route_Regex"
routes.resourceCSS.route = "resource/(\w+)/([a-zA-z0-9]+\.([a-zA-z0-9]+))"
routes.resourceCSS.defaults.module = 'default'
routes.resourceCSS.defaults.controller = "resources"
routes.resourceCSS.defaults.action = checkfiletype
routes.resourceCSS.map.moduleName = 1
routes.resourceCSS.map.fileName = 2
routes.resourceCSS.map.fileType = 3
controller name is resources
Follwoing is code of controller : -
class ResourcesController extends Zend_Controller_Action
{
public function checkfiletypeAction()
{
var_dump($this->_getAllParams());
$moduleName = $this->getRequest()->getParam('moduleName');
$fileName = $this->getRequest()->getParam('fileName');
$fileType = strtolower($this->getRequest()->getParam('fileType'));
switch($fileType)
{
case 'css' :
echo $fileType ;
$this->_forward('css') ;
break ;
case 'js' :
$this->_forward('js') ;
break ;
default :
break;
}
die();
}
public function cssAction()
{
var_dump($this->_getAllParams());
die();
}
public function jsAction()
{
var_dump($this->_getAllParams());
die();
}
}
Please help me.
You are passing in numbers as your filetype, and checking for words:
;ini file
routes.resourceCSS.map.fileType = 3
//controller
case 'css' :
Change the case to match the number:
//controller
case '3' :

How to allow custom flash keys in a redirect_to call in Rails 3

In Rails 3, you can pass has attributes directly to redirect_to to set the flash. For example:
redirect_to root_path, :notice => "Something was successful!"
However, this only works with the :alert and :notice keys; if you want to use custom keys, you have to use a more verbose version:
redirect_to root_path, :flash => { :error => "Something was successful!" }
Is there any way to make it so that custom keys (such as :error, above) can be passed to redirect_to without specifying it in :flash => {}?
In Rails 4 you can do this
class ApplicationController < ActionController::Base
add_flash_types :error, ...
and then somewhere
redirect_to root_path, error: 'Some error'
http://blog.remarkablelabs.com/2012/12/register-your-own-flash-types-rails-4-countdown-to-2013
I used the following code, placed in lib/core_ext/rails/action_controller/flash.rb and loaded via an initializer (it's a rewrite of the built-in Rails code):
module ActionController
module Flash
extend ActiveSupport::Concern
included do
delegate :alert, :notice, :error, :to => "request.flash"
helper_method :alert, :notice, :error
end
protected
def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
if alert = response_status_and_flash.delete(:alert)
flash[:alert] = alert
end
if notice = response_status_and_flash.delete(:notice)
flash[:notice] = notice
end
if error = response_status_and_flash.delete(:error)
flash[:error] = error
end
if other_flashes = response_status_and_flash.delete(:flash)
flash.update(other_flashes)
end
super(options, response_status_and_flash)
end
end
end
You can, of course, add more keys besides just :error; check the code at http://github.com/rails/rails/blob/ead93c/actionpack/lib/action_controller/metal/flash.rb to see how the function looked originally.