Rails 4.1 Create Page When Link Not Found - ruby-on-rails

I have a rather unique question that probably has a simple answer but I haven't been able to figure it out.
I am using rails 4.1 and creating an app that I want to accept urls that don't exist as part of a NFC tag registration process.
For example, let's say the app is located at example.com and I programmed an NFC tag with the url example.com/xyz123.
I don't want the rails app to spit out a 404 error but rather attribute that tag to the signed in User account and allow them to register that url to one of that users' personalized pages that they created.
I've dug into the docs on error handling but that doesn't seem to solve the problem and I can't seem to find where in the core API these requests are handled and how I might override them in the application.rb controller.

this is rather simple. just create a catch-all route
get '*path', to: 'tags#new'
the requested part will be params[:path]
i would recommend not having unscoped catch-all routes through. it's slow and a pita if you ever want to change your URL structure (you will want to do that at some point). always use some namespace www.some-url.com/t/xyz123

Related

Ruby on Rails: Trying to create an API that inserts records into table. Unable to accomplish it

I have created a rails app that periodically pings list of urls stored in a Server table. Now I want to create an API so that another application can insert records into the server table using this API. I researched about this topic on the internet to death and could not find a solution. Can someone suggest me how I can use grape gem to create this API? Any relevant blog articles would be helpful as well.
The Grape framework is an alternative to the Ruby on Rails framework, not something you add on top. Since you already have a Ruby on Rails app, you can do this more easily by adding this feature to your existing Rails app, which already has the ActiveRecord models and database connection set up.
The simplest way to do it would be to create a controller action add_url and make sure there is a route set up to it. Inside the action, run something like this:
url = params[:url] # get URL from HTTP params sent with the request
Server.create(url: url) # add row containing the URL to the Server table
Then tell users of your API to send a POST HTTP request to the URL that routes to the add_url action, and set the url parameter of that request to the URL to add the list.
Even better than having an add_url action inside some random controller would be naming the action create and putting it within a controller like ServerController that handles the creation, showing, updating, and deletion of URLs to visit. This would follow the REST convention of organizing APIs, which is more familiar to API users and which gives you a guiding principle to help you organize. However, if you already have a create method inside your ServerController that presents a web interface, you will have to make the action more complicated by using Rails’ respond_to feature to determine whether you should show a web page or respond to an API call.
Note that the above code has no security features restricting who can add URLs, so depending on your purposes may want to add that feature somehow.

Setup different frontend & backend application endpoints in Laravel 5

I've posted this on Laravel.io too, but no answer yet: http://laravel.io/forum/01-17-2016-setup-different-frontend-backend-application-endpoints
I've been struggling figuring this out but no luck yet - couldn't find an answer anywhere.
I have a regular public-facing website for which I need an admin interface. Assuming the website is at example.com, I want the admin interface to be accessible from example.com:3000.
I've tried domain routing, it doesn't seem to be working. The idea is to share all the business logic, but isolate assets and routes - for instance, accessing "/" would produce different results on the frontend (showing the homepage) and on the backend (showing a dashboard). I also need different middleware authentication, but I have a hunch that's going to be easy once I figure out how to set this up.
If it's easy to add different sessions, that would also be cool, but I can live without it.
This has been answered on laracasts: https://laracasts.com/discuss/channels/laravel/setup-different-frontend-backend-endpoints-in-laravel-51 , I'll quote from the answer here:
You can pull it off by editing the app/Providers/RouteServiceProvider
and loading in a specific routes file based on the "context" of the
application.
You could create two routes files /app/Http/Routes/frontend.php and
/app/Http/Routes/backend.php. You'll do some sort of logic in the
RouteServiceProvider to determine what route file to load in.

Rails: What is an API's endpoint?

I am doing the CodeSchool course on Rails API's and they often mention the word 'endpoint' but never define it. Can someone give a clear and concise definition of it and provide an example of a request reaching an end point in the context of Rails?
An endpoint, as I imagine they may be using it in this course, is simply a route defined by your rails application. In terms of an API (which can mean many things and is worth further research on your part), hitting that endpoint will serve up a resource from your application, or perform some form of action. An example may explain this better..
Say we have an application that handles users and we want our API to expose the users resource. If we follow RESTful convention for our API we will expose seven distinct 'endpoints' linked to seven distinct 'actions' (index, show, create, update, destroy, new, edit) surrounding users.
When building our API, we would make is so anyone who visits "www.myapp.com/users" via a get request is returned some data representation of all users in our application. "/users" is the endpoint. Likewise performing a post action to "/users" with valid data is how we create new users. "/users" is still the endpoint but under different context. If you wanted data for just a single user, it may look something like "www.myapp.com/users/1" in which case "/users/1" is the endpoint.
It is important to keep in mind that this example merely follows convention and is not an end all be all.
I would check out the Rails guide on routing if you want more information - http://guides.rubyonrails.org/routing.html
Resource https://edgeguides.rubyonrails.org/api_app.html they meant providing a programmatically accessible API alongside their web application

How can I omit controllers for two models in rails 3 routes?

I'm building a rails3 application and at the moment I have the following line in my routes.rb file:
get "/:id" => 'tapes#show'
In other words, you can show a Tape using website.com/tapes/1 and also by using website.com/1
(I am also using friendly_id gem so the user sees in fact a friendly URL in the form of website.com/tapename)
Now, what I would like to achieve is to do the same thing for Users pages. So instead of showing a User page using website.com/users/alex I want to be able to also use website.com/alex.
Is there a way to implement this 'users' logic in routes.rb together with the existing 'tapes' rule and somehow set a priority?
So if someone accesses website.com/alex my app would search if there is a User record with id 'alex' and if none is found then look for a Tape with id 'alex'.
Could I use some kind of Advanced Constraints in routes?
Any ideas?
Many thanks for the help,
Alex
Rails would have no way to determine which controller you were trying to access. The only way that this would be possible, is if either:
you could determine which model it would resolve to based upon some regular expression on the name.
or
You knew that user names and tape names never conflicted, and were willing to suffer the cost of hitting the database to resolve the correct controller.
This is probably a bad idea for a number of reasons, it would have performance implications, it also doesn't conform to RESTful principles, and it would probably be confusing to users.

ASP.NET Multi tenant route configurations

This is not a question of how to implement multi-tenancy. What I am looking for is the ability to optionally override the default application routes when required.
A simple case is that I have a route that generates a blog url like so:
http://[domain]/blog/post-slug
My configuration is blog/{slug}
However, some tenants do wish to call their blog "news". Although I can change all references blog in their views, I can't do it in the URL.
I would like a simple way of overriding these route configurations without changing the "core" codebase.
One thought I had would be to use named routes and then use this to swap out route configurations when the app starts. I should add that multiple tenants will not be using the same application instance, just the same codebase.
Thanks,
Ben
You can always add a route using a regular expression. There is also a great tool on figuring out what routes to add to get what you want.
http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx
What about if you have a filter that detects which type of user is accessing the action. If it is one who wants the word news, you can redirect them to a controller called news - which inherits all the functionality from blog.
I can't say I'm 100% confident, but I think it might work.

Resources