Defining Multiple API Routes in Laravel 5.1 - laravel-5.1

I have defined an API route in Laravel like this
Route::group(array('prefix' => 'api'), function() {
Route::resource('getdealsbymerchant/{merchant_url_text}', 'dealsController#index');
});
This route is working fine and returning me a response. I want to add another route like this in the group
Route::resource('getsimilarmerchants/{merchant_id}', 'similarmerchantsController#index');
However, when I add this and try to hit
http://localhost/api/getsimilarmerchants/123
it is giving me an error
Route pattern "/api/getdealsbymerchant/{merchant_url_text}/{{merchant_url_text}}" cannot reference variable name "merchant_url_text" more than once.
Any help on how I can fix this?

Why do you have a resource controller pointed to a method?
It should be
Route::resource('getdealsbymerchant', 'dealsController');
Similarly define the second resource too. Read more here : http://laravel.com/docs/5.0/controllers#restful-resource-controllers
If you just want a get/post route, define as follows
Route::get('getdealsbymerchant/{merchant_url_text}', 'dealsController#index');

Looks like I was doing it wrong.
Defined 2 routes like this and was able to resolve the issue.
Route::get('api/getsimilarmerchants/{merchant_id}', ['uses' => 'similarmerchantsController#index']);
Route::get('api/getdealsbymerchant/{merchant_url_text}', ['uses' => 'dealsController#index']);

Related

how to fetch the route i.e path of my current api in routes.rb file

I'm new to rail. I have few clarifications, which I have listed below.
I have referred many links and stack overflow questions, everywhere it's mentioned to use request and fetch the details regarding the path, controller, action etc. but if I use request in my routes.rb it throws undefined local variable or method error.
I used constraints in my routes.rb and from there it calls a method matches? from a class where dynamic constraints are defined inside lib/constraints directory. In here the matches?(request) receives a parameter named request, which has details about current route, from where the parameter value is sent?, I have this doubt because while using this method inside routes.rn in constraint(ClassName) I'm not sepecifying the method name (matches?) or the parameter request
I would like to know the working of things behind the scene.
Thank You
Are you looking for it
request.url
# => "http://localhost:3000/lists/7/items"
request.path
# => "/lists/7/items"
If you have URIs:
localhost:3000/users/:id
localhost:3000/users
localhost:3000/users/new
....
You can do something like
rake routes | grep user
in order to get only those that contain user in its path name.
Hope this helps..
conventionally you don't access the current path in the routes.rb, at least as how it seems you're asking to access it. There are a number of different matching methods and techniques used in the rails router that serve the utility I imagine your looking for.
Here's a link to the docs: https://guides.rubyonrails.org/routing.html
Can you share exactly why you want to access the curret route in the routes.rb file?

change url name in rails

I have a route in my rails application:
get 'welcome/usertypeone' => 'welcome#usertypeone'
This doesnot look good in the browser as the URL comes out like:
www.mywebsite/welcome/usertypeone
Any way I could change the way the url looks to something like:
www.mywebsite/welcome/teacher
without needing to change the route itself as i would need to change it in loads of places. Just seeing if there is a better solution to this.
If you're explicitly defining the route (as you are, as opposed to using resource etc) you can configure the route as you'd like.
get 'welcome/teacher' => 'welcome#usertypeone'
This will make www.mywebsite.com/welcome/teacher route to the same controller and action.
You will, however, need to update the route throughout your application from welcome_usertypeone_path to welcome_teacher_path. Your text editor probably has a search and replace function making this a 10 second step.
Maybe what you want looks like this:
get 'welcome/:user_type' => 'welcome#usertypeone'
This will take whatever is after the welcome/ and put it in params as params[:user_type], but it will still go to the WelcomesController and the usertypeone action.
You can go to your routes.rb file and try:
get 'welcome/teacher' => 'welcome#usertypeone'
Then you should get the url you require: www.mywebsite/welcome/teacher

Rails - prefixing route helpers

I am wondering what would be the best way (or indeed any kind of way) to prefix path/url helpers that get generated by Rails?
I.e. I need my dogs_path equivalent of having "extra/path" + dogs_path
I am not looking to namespace my routes (i.e. scope '/admin' { #my_routes }), this is needed specifically to address some reverse proxying issues that I'm having.
Any help would be appreciated!
Why not simply define your route with as option like this?
get 'dogs' => 'dogs#index', as: 'extra_path_dogs'
This will give you:
extra_path_dogs GET /dogs(.:format) dogs#index
And, you can use extra_path_dogs_path afterwards.

In rails how do I route domain.com/12345 to /fetch/12345 dynamically?

I'm having a little trouble understand how routes work in Ruby on Rails.
What I'm trying to achieve is have all ID's accessible directly after the domain name,
for instance
domain.com/<- ID goes here->
routes to
domain.com/fetch/<- entered ID ->
Any push in the right direction would be greatly appreciated.
Thanks a lot
This might be a bad idea; once you put in this general route then any unrecognized url with a single path component is going to end up being handled by your fetch method. Assuming you understand and are okay with that there are several ways that you could do this, the most straightforward:
I assume that you already have /fetch/:id in your routes, something like this to handle /fetch requests in ApplicationController#fetch:
namespace :fetch
get '/:id' => 'application#fetch'
end
Then you can add a rule at the bottom of your routes like this:
get '/:id' => 'application#fetch'
That should go at the very bottom because you don't want it to override any more specific single-path-component routes.

Sending a variable through a link_to without url query in Ruby on Rails

I'm trying to send a variable through a link_to, without using url query in Ruby on Rails, from one controller's index view to be used on another controller's index.
Basically, I need to pass the Turma.id (school class id), to the Elementos_turma(peoples in the class) controller, so I can use it and filter the index: ElementosTurma.find(:all, :conditions => { :turma_id => xxxxx } ), to show the peoples of the selected class.
It it possible?
Maybe without using a session variable?
Maybe sending the variable to a method on the 1st controller, to send it the other controller? (if so how? not very RoR wise... :) )
No need for a special method to get the info you need, the magic of routes will do.
In your routes.db file, you should be able to define something like this,
map.resources :class, :has_many => :students
Then if you run 'rake routes' you should see a routes similar to this
class_students GET /classes/:class_id/students(.:format) {:controller=>"students", :action=>"index"}
You can call that path in your view like so
class_students_path(class_id)
Then in your controller you will have access to params[:class_id]
The name of the route isn't very pretty, but this should work.
EDIT--------------------------------------
According to your comment, you can't use map.resources for some reason or another...
map.class_students '/:class_id/students', :controller => 'students', :action => 'index'
That will produce the same route available in your view, same param in your controller.
That being said, I don't know how a server bug could prohibit you from using map.resources
You can not transfer data through a link without including that data in the link.
However, it sounds like you just need to be using nested resources. (Since I speak English, I'm going to not tackle another language and do what comes to me.) The URLs you want to be sending people to probably should look more like this if you want to be RESTful:
/classes/1/people/
That is the "Rails way" of indicating that you want to get people in class #1, and Rails offers built-in routing methods to make this easy. See the Rails Routing from the Outside In article in the Rails Guide.

Resources