RESTful way to route employees workhours by month - ruby-on-rails-3

i'm wondering what route to choose.
I got a view containing employees workhours, and the route file looks like this:
resources :employees do
resources :workhours do
end
so the url is:
/users/:user_id/workhours/:id
What i want to do is only showing monthly hours with links to next and prev month.
What is the most RESTful way to do this? I have read that you shouldn't nest the url to deep, but i don't know.
Thanks in advance.

Related

Get data from last 3 days from NASA API

I am using api.nasa.gov and trying to get data from last three days. But I cannot find what should I add in the url. Cannot find the option to filter out the result.
Can anyone help me out?
This is the url I am using
$url = "https://api.nasa.gov/neo/rest/v1/neo/browse?page=0&size=50&api_key=api_key";
docs on the website state
documentation
prop start_date
prop end_date
https://api.nasa.gov/neo/rest/v1/feed?start_date=2015-09-07&end_date=2015-09-08&api_key=DEMO_KEY
GL :)!

laravel 4 pattern filter using a wildcard

I started using Laravel 3 last week, and then found the new 4 release and I'm trying to convert now.
I have a dozen+ routes that I want to deliver to a specific controller method. i.e., "/api/v1/owners/3/dogs/1 or /api/v1/owners/3" to run "myresourcecontroller#processRequest"
In Laravel 3 I was able to use this: (note * wildcard)
Route::any('api/v1/owners*', 'owners#processRequest'); // Process tags resource endpoints
I found this example from the documentation but it gives me an error. I get a NotFoundHttpException.
//[Pattern Based Filters](http://laravel.com/docs/routing#route-filters)
Route::filter('admin', function()
{
//
});
Route::when('admin/*', 'admin');
Not sure what I'm doing wrong? Is there another way to do this?
I don't want to use the Laravel 4 restful controllers, cause they don't seem to conform to complete restful design. i.e., no verbs in the url.
I have all of my processing written, I just need to be able to route to it.
I need to be able to create new records by POST /api/v1/owners or /api/v1/owners/3/dogs
I cannot use /api/v1/owners/create.
I'm trying to avoid having to write a route for every endpoint, i.e.,
Route::any('api/v1/owners/{owner_id}', 'owners#processRequest');
Route::any('api/v1/owners/{owner_id}/dogs/{dog_id}', 'owners#processRequest');
Thank you for any help
You should make use of resourceful controllers as they're a great asset when building an API. The endpoints you described can be achieved using resource controllers and nested resource controllers.
Route::resource('owners', 'OwnersController');
Route::resource('owners.dogs', 'OwnersDogsController');
Would allow you to create an owner with POST localhost/owners and create a dog on an owner with POST localhost/owners/3/dogs.
You can then wrap these routes in a route group to get the api/v1 prefix.
Route::group(['prefix' => 'api/v1'], function()
{
Route::resource('owners', 'OwnersController');
Route::resource('owners.dogs', 'OwnersDogsController');
});
Haven't used Laravel myself, but try any('api/v1/owners/*', (note slash before asterisk) as in the example.

Multiple Resource Routing In Rails

I have two unrelated models, say Person and Building. When the app receives a url like www.mysite.com/JohnDoe/EmpireState I would like to show properties of the Person with the name johnDoe, and the same for the building with the name EmpireState.
I'm confused as to the routing part specifically. I'm unsure if I need to create a pages controller that can return the objects from the database. How should I go about doing this?
Am hoping for something like below?
match ':user_name/:building_name', :controller => pages
If those two are not related, you shouldn't do it that way. If they ARE related, we call that nested resources.
Example:
resources :projecs do
resources :tasks
end
Sample URL: "/projects/12/tasks/1281"
Edit:
If they are NOT related (taken from my comment):
In your BuildingsController you can fetch the parent informations too. If you use the match route in your question, you'll have params[:user_name] AND params[:building_name] available and can fetch anything you want with them...
Building.find_by_name(params[:building_name]) # return all Buildings based on URL param

Rails-3.1 nested routes creating same action and controller, basic lack of knowledge

Doing the following routes configuration:
resources :cadeiras do
resources :professores
end
resources :cadeiras do
resources :fichas
end
resources :fichas do
resources :exercicios
end
will generate me 2 different links to the same controller and action, running rake routes ill get something like:
fichas GET /fichas(.:format) {:action=>"index", :controller=>"fichas"}
cadeira_fichas GET /cadeiras/:cadeira_id/fichas(.:format) {:action=>"index", :controller=>"fichas"}
The first action will reference all the 'fichas' while the second on is referencing only 'fichas' from 'cadeiras' how is it possible to distinguish the two actions?
I would like to avoid three level nesting problems as described here :http://weblog.jamisbuck.org/2007/2/5/nesting-resources
Thank you for your time
If I understand your question correctly, the answer is "you don't distinguish them" :
The exact same action is executed from the controller, rendering the exact same view. The difference is the collection of 'fichas' that get sent to the view:
- in the first case, all fichas are available in the view
- in the second case, only the 'fichas' related to the 'cadeira' are available in the view (e.g. /cadeira/1/fichas will display only the 'fichas' related to the 'cadeira' with id 1)
To determine which records to show (e.g.) in an index view, you can do something like this:
unless cadeira_id = params[:cadeira_id]
#fichas = Ficha.all
else
#fichas = Cadeira.find(cadeira_id).fichas
end
The rest is up to the view: it should render fichas the same way, you just chose which records are actually made available to it.

Rails 3 - how to make a crossing? (routing)

I created a controller names. I have a model that contains a names of boys and girls (gender=0 for boys, 1 for girls).
If I set to URL the address localhost:3000/names, so will be rendered the view index.html.erb. In this view is an overview of data that are stored in database.
I am trying to edit it - I want to have on the address localhost:3000/names a crossing - here will be 2 links - BOYS and GIRLS. And after click on one of these links I would like to go on the address localhost:3000/names/girls (or boys) and here I would like to have an overview of data from database...
I am newbie still and I don't know, how to realize it... mainly how to edit my routes.rb - I would like to ask you about a help, how to do it...
Thank you in advance
So you want a names_controller with actions boys and girls, and you want routes to those?
How about something like this:
resources(:names) do
collection do
get :boys
get :girls
end
end
Routes collection is documented here.