I'm building an api in rails, and I have been told that I need to scope the site so it has versions.
Something like /1.0/do_something.
From what I've read, I do this by making a routes.rb that looks like the following:
API::Application.routes.draw do
scope :path => "1.0", :module => "1.0" do
post "do_something" => "controller#method"
# Everything else. Glob is saved in params[:r]
match '*r', :to => 'errors#e404'
end
end
But when I rake routes, I get the following error:
rake aborted!
missing :action
What am I doing wrong?
You cannot have a module called "1.0" for obvious reasons. Consider calling this V1 instead.
Related
I'm working on an existing sinatra app that spans a number of controllers. These are currently routed in the config.ru file like so:
require 'bundler'
Bundler.require(:default)
=begin
# The gem file includes these:
gem 'rack' , '1.5.2', :groups => [:default, :test]
gem 'rack-accept' , '0.4.5'
gem 'rack-mount' , '0.8.3'
gem 'rack-protection' , '1.5.1'
gem 'rack-test' , '0.6.2', :groups => [:test]
=end
use Rack::ContentType
maps = {
'/' => RootController,
'/users' => UsersController,
'/animals' => AnimalsController,
}
maps.each do |path, controller|
map(path){ run controller}
end
This file is launched via config.ru (under Thin). But test files don't run under rackup (or Thin).
How do I load the controllers under rspec?
The problem is that when I put a 'get "/PATH"' or 'post' in my tests, Ruby complains about an
argument mismatch (2 for 0). If the 'get' has no argument, Ruby gives a different argument
mismatch (0 for 1). They're both sort of wrong -- get takes a path, an optional hash, and an optional block.
So something is clearly not wired up correctly.
Some of the code is here:
config.ru: http://pastie.org/8673836
spec_helper.rb at http://pastie.org/8673835
Error message at http://pastie.org/8673793
Simple controller at http://pastie.org/8673785
The names are slightly different in the pasties, but the gist is the same.
How do you wire up the controllers when you don't have the environment
config.ru gives you?
Thanks for any help.
The problem is somewhere in the code base, but rack doesn't make it easy to determine where.
I filed a bug on this at
https://github.com/rack/rack/issues/652
I am developing a simple web app using Sinatra and using rack as the middleware and hence have a config.ru.
To run the application I use shotgun config.ru.
I have no problem when the application does a GET request. But my app has a couple of POST requests, and when I submit a form via POST method, I get this strange error:
Method Not Allowed
Following is the content of my config.ru:
require "rack"
require 'rack/contrib/try_static'
require File.expand_path("app", File.dirname(__FILE__))
use Rack::TryStatic, :root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']), :urls => %w[/]
run App
Any idea what could resolve the issue?
Thank You
The following will not respond to posts:
get '/hi' do
"Hello World!"
end
It is quite possible that you will need to do something like this:
post '/hi' do
# do post stuff
end
I solved the issue.
It was a problem with rack.
I replaced
use Rack::TryStatic,
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination']),
:urls => %w[/]
with:
use Rack::Static,
:urls => ["/#{App::SETTINGS.site.config['destination']}"],
:root => File.join(App::SETTINGS.source, App::SETTINGS.site.config['destination'])
My Sinatra web site uses an admittedly ghetto way of switching between two languages, which works perfectly on my local machine, but not on Heroku.
My app.rb is as follows:
require 'sinatra'
get '/' do
set :erb, :views => settings.views + "/en"
erb :index, :layout => false, :views => settings.views
end
get '/tr' do
set :erb, :views => settings.views + "/tr"
redirect to('/home')
end
get '/en' do
set :erb, :views => settings.views + "/en"
redirect to('/home')
end
get '/home' do
erb :home
end
get '/products' do
erb :products
end
...
When I click on the links that go to the /en and /tr urls, I am correctly redirected to the /home link, but the views folder does not change.
My gems are the same versions in both environments:
rack (1.4.1)
rack-protection (1.2.0)
tilt (1.3.3)
sinatra (1.3.3)
bundler (1.3.2)
Link to the site
Help, please.
I suggest replacing your admittedly ghetto way with something more robust like the i18n gem
here is a tutorial
And instead of using the browser's language (in example) you could do something like this in a before filter
before do
case request.path_info
when /^\/tr/ then set :locale, "tr"
else set :locale, "en"
end
end
and in helpers
helpers do
def get_locale
settings.locale
end
# other helpers from example
end
That way you only need one copy of your view templates, reducing the complication of changing all views when you only need to change one element (for example)
If you want to test your sinatra app like it will act on heroku you can throw on a RACK_ENV=production before starting your server. Ex. RACK_ENV=production ruby my_app.rb
So I have some code for running a batch file as a specific user. This was my attempt to automate the following syntax
runas /user:thisguy "C:\ThisGuysScript.bat"
so it looks like this in Ruby
Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
So I try to put this in a recipe in chef and disaster strikes
require 'win32/process'
::Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
Is failing with the following error
[Tue, 30 Oct 2012 15:57:03 +0000] FATAL: ArgumentError: You must supply a name when declaring a user resource
So it seems to not realise that I want to use the win32 flavour process. Chef seems to override the win32 module (I know recipes are the opscode DSL rather than really ruby right?)
Anyone been able to get this working? Or the same function with a different implementation. Checked out the windows cookbook but didn't spot much
It sounds like you want to make an LWRP for creating a process on a windows machine.
The error you are getting means you have something like
user do # Missing name
gid 500
home "..."
end
the correct syntax is
user "apache" do # or whatever the user name should be
# ...
end
If you don't have the above in your cookbook, it is possible that the included file has a variable named user which would also cause this issue.
To answer your subquestion, Chef is straight ruby with some functions made available and a frame work to run things. Note, there are several stages in a chef run. I think you are having issues in the compilation stage.
Making an LWRP seems like the way to go. If you don't want to go that far you could do something like.
ruby_block "Firing process lazers" do
require 'win32/process'
::Process.create(:command_line => "C:\\ThisGuysScript.bat ", :domain => "MYServer", :with_logon => "thisguy", :password => "thisguyspassword", :cwd =>"C:\\")
end
I have a rails app with a folder rails.root/app/resources where I keep some library code.
This library code is used in a rack app that I mount in routes.rb in my Rails application
# rails.root/app/resources/file_resource.rb
# routes.rb
mount DAV4Rack::Handler.new(
:root => Rails.root.to_s,
:resource_class => FileResource
), :at => '/', :constraints => {:subdomain => "w"}
How can I make the FileResource reload on each request in development?
I tried autoload_paths, reload_plugin, none seem to work.
I think it has something to do with the code in routes.rb.
If I make a file rails.root/app/resources/my_helper.rb and use MyHelper.test() inside FileResource, the MyHelper gets reloaded.
No, I don't want to move this in 'lib' folder.
I am currently using the following hack, if anyone has a better solution please do share!
# config/environments/development.rb
root = config.root
config.to_prepare do
load "#{root}/app/resources/file_resource.rb"
end