I need to create route /branch which run script /lib/branch.rb, I need to display only one string which displayed in browser my current git branch which deployed at staging server. I already write script, but cant understand how to mount it to rails routes
Rails routes dispatch requests to the application. You can put your code in either a Controller or a Rack app (Rails 3 only).
If you are using Rails 3, you can use the Rack API to do it right inside your routes.rb.
match "/branch" => proc { |env| [ 200, {}, [ %{your script call} ] ] }
Take a look at this post: http://www.railsdispatch.com/posts/rails-routing
Rails routes are routing the request to controllers.
So put your code in /app/controllers/branch-controller.rb
Related
my project work very well in local server but when i publish it in live hosting everything work except the oauth2 not work and give me Route [login] not defined
i dont khnow were is the problem
my .htaccess
my Route api
Laravel redirect you to login route when API request doesn't authenticated, so add this line to routes/web.php file:
Route::post('login', [ 'as' => 'login', 'uses' => 'LoginController#do']);
I am using sentinel by Ryan Durham https://github.com/rydurham/Sentinel with Laravel 5.1.
I used to use Confide https://github.com/Zizaco/confide with Larvel 4.2.
With Confide I was able to add an auth to any route like this
// Applies auth filter to the routes within orders/
Route::when('orders/*', 'auth');
I've searched through Sentinel documentation for this feature but I cannot find out how to get this done. I can put an auth for say admin or user in the construct of a controller, but I need to have specific auth abilities on specific routes.
My question is, how can I redirect a user who goes to a route only for admin using rydurham sentinel in a clean fashion the same as Confide?
You should use sentinel middlewares on routes. Something like
Route::get('orders', ['middleware' => 'sentry.admin', function () {
// return view;
}]);
I'm building an API with Rails 4 and I really want to create a Batch request method to not overload my app when doing a bunch of ajax requests.
The Railscasts shows how to do it in a simple way but there's a lot of stuff missing.
I also tried the batch_api gem but I wasn't successful integrating it with my application.
Any ideas?
I know it's being late to answer this question but I recently used batch_api gem with my Rails API (rails 5.0.0 and Ruby 2.0) and it works find with me.
What you need to do is follow the instruction of this document:
https://github.com/arsduo/batch_api
Shortly:
1- You need to add the batch_api gem to your application GemFile.
2- You need to add the required middleware configuration in you application.rb file:
config.middleware.use BatchApi::RackMiddleware do |batch_config|
# you can set various configuration options:
batch_config.verb = :put # default :post
batch_config.endpoint = "/batchapi" # default /batch
batch_config.limit = 100 # how many operations max per request, default 50
# default middleware stack run for each batch request
batch_config.batch_middleware = Proc.new { }
# default middleware stack run for each individual operation
batch_config.operation_middleware = Proc.new { }
end
3- Then restart your rails server.
Make sure to insert the new middleware in the appropriate location, in my case I needed to include it before "ActionDispatch::RequestId" middleware.
config.middleware.insert_before "ActionDispatch::RequestId", BatchApi::RackMiddleware
because I wanted to include X-Request-ID header in each request in the Batch request and this ID will be returned in each response so that I could know the response for each request in the Batch (note that the responses will be executed sequentially depending on the sequence each request in the Batch).
Apparently the batch_api gem doesn't work with rails 4 yet, but there is a fork that was started to update it to rails 4 and ruby 2.0.
https://github.com/easyPEP/batch_api/tree/feature_ruby_2
I am trying to create a restful web service in a rails application. I need to configure a restful routes as below.
localhost:3000/books/<book_name>/new/<parameters to create Book Item>
Eg: http://localhost:3000/books/sherlock/new/id/123/business/<BU>/.../{other parameters}
How do I configure it in the routes and access the params in controller. Please help.
This technique is called "route globbing".
http://guides.rubyonrails.org/routing.html#route-globbing-and-wildcard-segments
However, if I were you - I'd used "default" query params. For many reasons.
I am trying to split my current Ruby on Rails 3 web-application and it's web-services (API). My web-application is running on Heroku and implements API as a namespaced route within my application. For example /events returns a HTML page and /api/v1/events returns a JSON data.
According to some best practices, I want to split those into two different applications. I have chosen Sinatra to implement the API application. It works now for simple requests where authentication is not required.
My Ruby on Rails 3 application is using Devise to authenticate users. There's also ability to login with Facebook account. Now what I want to achieve, is HTTP Basic Authentication of users (including registration) through my Sinatra-based API by using Warden.
What is the best way to do that? Or maybe I can use something different then Warden?
Keep in mind that I am not very familiar with Rack :)
I was able to get it working. There were a few main aspects:
Get Devise working with Rails (Devise is a Rails app, won't work
without it)
Setup the mapping (route) on Rack level to support both Rails and Sinatra
Share the sessions between Rails and Sinatra
Setup Warden and make it available to Sinatra
Here is most relevant part of code from /config.ru:
#
# ...
# Rest with Rails
map "/" do
run MyApp::Application
end
# Anything urls starting with /slim will go to Sinatra
map "/slim" do
# make sure :key and :secret be in-sync with initializers/secret_store.rb initializers/secret_token.rb
use Rack::Session::Cookie, :key => '<< see, initializers/secret_store.rb >>', :secret => '<< copy from initializers/secret_token.rb >>'
# Point Warden to the Sinatra App
use Warden::Manager do |manager|
manager.failure_app = AppMain
manager.default_scope = Devise.default_scope
end
# Borrowed from https://gist.github.com/217362
Warden::Manager.before_failure do |env, opts|
env['REQUEST_METHOD'] = "POST"
end
run AppMain
end
See, http://labnote.beedesk.com/sinatra-warden-rails-devise for a complete solution.