Rails and Angular Routing - ruby-on-rails

So I have a rails app, with angular being slowly introduced. IM having an issue with rails routing and angular ui-route playing together nicely.
I have a order/index.html.erb that has
<div ng-app="orders">
<div ui-view></div>
</div>
That ultimatley routes to a partials/orders/users/index.html file. Within that file I have a link to the edit page, that looks like this.<a ui-sref="users_orders_edit({userId: order.user.id, orderId: order.id})">Edit</a>
That link is being routed by angular to an edit page. Here is the issue. When I refresh the edit page with the url /users/:id/orders/:order_id/edit, it throws a rails error, saying that route is not defined.
What am I missing here?

Here's what I do to solve this. In your routes.rb file add the following towards the bottom of the file so that it doesn't match every route:
get "/(*redirect_path)", to: "the_controller_that_serves_your_angular_app#index"
You don't have to call it redirect_path. It can be named whatever you like because you won't use it. This is telling rails to map all uncertain paths to your angular app. This will allow your users to refresh and it still load the angular app and even go to the correct state.

Related

How to make a URL displayed in Activeadmin clickable

In one of my ActiveAdmin pages I have fields containing a URL (either https: or file:). How can I make that when clicked on a new browser tab opens with the corresponding content ?
I tried
link_to('site web', :siteWeb)
but the result is an error message:
undefined method `siteWeb_path' for #<#<Class:0x00007f048201edb0>:0x00007f048202f700>
The problem here is your using a path helper that isn't defined in your routes.rb file. There's an easy way to see what the right path helper is though, just go to your terminal and run:
rails routes
You'll get a bunch of information about each route, along with the specific name of the route path helper function defined in Rails. Then you can just use it like so: https://apidock.com/rails/ActionView/Helpers/UrlHelper/link_to

frames routing in erb rails

Two questions.
Number 1:
This is how I am creating the frames in .erb files.
<frameset rows="170,*" frameborder="0" border="0" framespacing="0">
<frame name="topNav" src="top_nav.html">
</frame>
</frameset>
I have couple more frames inside above frameset. If I run this code, I get "No route matches [GET] "/top_nav.html". Question is: Is it really necessary to create/config routes of all the src locations specified in the frame tag?
Number 2:
In my application, I would need to change links in my app very frequently. If I need to consider the routing, I would need to create routes more frequently. Right? How can I avoid consider the routes? Basically, If I add any link in my app, it should work irrespective of fact that whether I have added the routes or not.
You can put a static html page in your /publics folder, which will bypass rails routing and be served directly if e.g. someone visits website.com/top_nav.html, it'll serve up public/top_nav.html. The reason you're getting this error without visiting it is because the frame tries to load the src when you load the frame.
If you just want to serve a blank page as a catch-all for missing pages, you could create a controller that catches anything without another matching route, like so:
# config/rotues.rb
# make sure this is at the BOTTOM of your routes.rb
match "*path", to: "application#default", via: :all
And then in your application controller:
def default
render nothing: true
end
This probably isn't the best way to handle it, though. You'd be best having the links you want created and served dynamically, but that'd depend on what your application is doing.

Rails 4: Any way to fix/use regular html href links w/o 'link_to'?

So my question is that I have a link to 'pages/home' and I click on it, ill go to my home page.
But then I'll try to click again, but the link changes to 'pages/pages/home' and then I'll get a routing error. Is there anyway to fix this using regular old anchor tags? or do i need to use link_to?
edit:
This is how i insert my link into the page.
Home
This is not related to rails, the problem is you use a relative url :
Home
This will lead to <any_path_you're_in>/pages/home.
For it to be absolute, you have to use (note the leading slash):
Home
By the way, it's quite a bad practice to use hardcoded url to your own rails app. You can avoid using #link_to while still taking advantage of rails' routing :
Home
Provided you have a "home" route, of course :
get '/pages/home' => 'pages#home', as: 'home'
This will save you a lot of pain when you decide to restructure your app.

Rails - Storing route data in database

I have a model that stores a path in the database under a column named "link_path" - for example:
Model.first.link_path == /posts/1
(or, put another way, I am caching the controller/model_id in a database table so Rails doesn't have to build it from scratch.)
In my view, I would like to build up a URL using this path information - for example, http://www.mysite.com/posts/1
I currently have the following code in my view:
<a href="<%= "#{request.protocol}#{request.domain}#{request.port_string}#{post.link_path}" %>">
In development all works as expected both when using POW/Nginx and Webrick - e.g., the link builds up to
http://localhost:3000/posts/1).
However, in production, when I hover over the link, it shows correctly (e.g., mysite.com/posts/1) BUT, when I click on the link, the '/' between .com and posts is strangely missing. The page links to http://mysite.composts/1
Any thoughts on how to fix?
Thanks to BrMcMullin - I wasn't aware I could use the URL helpers without calling the model. Since I don't need the weight of link_to, I ended up changing to
url_for(post.link_path)
instead of manually building up using string interpolation and that seems to work as expected.

Rails 3.1 build GET form that creates a custom URL route that is SEO friendly

I would like to create custom SEO friendly routes similar to what is used by http://realestate.com.au For example the following page is shown by google when the search term "real estate melbourne" is used:
www.realestate.com.au/buy/in-melbourne,+vic+3000/list-1
I would like use the following format. mysite.com/trips/search/melbourne-to-sydney/01-01-2011
I have configured the routes in my routes.rb file to get it to pick up the correct parameters when a url is entered is this format.
routes.rb
match '/trips/search(/:fl(-to-:tl(/:tripdate)))' => 'trips#someaction'
My question is how do I setup a form in rails 3 to send a GET request using the above url structure. I have tried playing around with to_params though it seems to then change all my edit, show links etc which is not intended. I could build the link using javascript though I guess this would be a hacky option and the site would not work if javascript was disabled.
Is there a neat way to be able to create a GET submit form in Rails 3.1? The fields are select lists containing name and ids.
Thanks for your help.
This will help you immensely with the friendly URL portion
http://norman.github.com/friendly_id/file.Guide.html
https://github.com/norman/friendly_id

Resources