How can I route to index.html.erb? - ruby-on-rails-4

I need route to index.html.erb? How can I do it? I tried root "index.html.erb#index" in routes.rb.

First of all, in rails we set routes for our controller's method not for our
view file. Get method of controller redirect to view the page itself.
Write below code in your routes.rb:
get 'index', to: 'controller_name#index'
You can replace get 'index' with any name like get 'users'.
You can refer this for more reference.

Related

emberJs giving different routes to links

I got a page that includes 15 pictures and I must redirect user to the details page if one of them is clicked. so, I have 15 different routes like 1,2,3,...15. But as I know, I cant give dynamic variable to #link-to helper. So how am I gonna make every picture go to its own route? this is how my index page .hbs looks like :
{{#each model as |rentalUnit|}}
{{rental-listing rental=rentalUnit}}
{{/each}}
{{outlet}}
This is how I print every picture. And this is how am I trying to go to routes :
{{#link-to "???"}}<img src={{rental.image}} width="200">{{/link-to}}
so, there is ??? because I dont know what to do. Thanks!
Well, first why not use a dynamic segment in your route? Like this:
this.route('image', {path: '/images/:id'});
But what you want can be done by just passing a variable, not a string to the link to helper as first argument:
{{#link-to target}}
Where {{target}} would print your route name. So target is a variable here, not a string.
Checkout this twiddle.

Set URL for Submit Page in Controller Rails

So I have this url in my site
localhost/home-loan
And when clicked, it performs the following function from the controller page
mortgage_loans_controller.rb
I then fill up a form and click submit. But i want my submit page to have the following url,
localhost/home-loan/thank-you
But it has localhost/mortgage_loans/thank_you
thank_you is also another function in the mortgage_loans_controller.
How can i do this?
Use this:
match '/home-loan/thank-you', to: 'your_controller#your_action', as: 'custom_name_for_helper', via: [:post]
Place this line inside of the routes.rb.
This line says that whenever you doing POST request to the localhost/home-loan/thank-you, execute your_controller's your_action.
as: options will use 'custom_name_for_helper' to create path helper, like custom_name_for_helper_path.
via: option determines which request method used, if you need to handle all types just use via: :all
I don't know the structure of your routes.rb, so place this line above all routes, to be sure that it will work.

Inject variable from the views to my angular controller?

I'm trying to create a website with articles. I have a page that displays a list of all the articles, and I try to do one that displays the detail of an article. I use angular.js to get the json of my datas. I don't have any problem to get the list of my articles since I only need to do :
function ArticleListCtrl($scope, $http) {
$http.get('/articles/?format=json').success(function(data) {
$scope.articles = data;
});
}
But now I only want to access for example the article with id 4. How do I do that ? Is there a way to inject the primary key entered in the url into the javascript ? I'm new to angular, and I'm pretty sure there is an easy way!
If I understand correctly, what you need is to define a partial template and a route,
for the detail view of your article.
An example is here
More specifically what you'd need is something like the following.
angular.module('myapp', []).
config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/articles/:articleId', {templateUrl: 'partials/article-detail.html', controller: ArticleDetailCtrl}).
otherwise({redirectTo: '/articles'});
}]);
What the above does, is to hijack urls of the form /articles/5/ and instead of actually
performing that GET request to your server, it will just ask for partials/article-detail.html. This of course will be your article detail template, which will be handled
by your ArticleDetailCrtl controller.
In your ArticleDetailCrtl controller function, don't forget to include the $routeParams
service. This will give you access to the url parameters, such as articleId, which we
defined above.
The final thing to do is to generate these links in your article list template. e.g:
<ul>
<li ng-repeat="article in articles">{{article.title}}
</ul>

How to pass current request params to Symfony2 route

I'm trying to create language/locale switch urls using Symfony2 and twig which will allow to switch language for route without resetting url:
http://example.com/en/cat/test
http://example.com/it/cat/test
http://example.com/sp/cat/test
...
and so on
Here is revelant part of my twig template:
English
My problem is that for example using category url(http://example.com/en/cat/test) gives me exception:
The "_category" route has some missing mandatory parameters ("category").
Which is all okay and understandable since for switcher urls I'm passing only _locale param.
How to pass all current url params?
You can get all request attributes from app.request and override part of them.
<a href="{{ path(app.request.attributes.get('_route'), app.request.attributes.get('_route_params')|merge({'_locale':'en'}))}}">

Url mismatch, django template link

I've got a prob with a link in the sidebar of my django site, in the template it's like that:
<li>Profile</li>
while in the urls.py:
url(r'^(?P<user_id>\d+)/profile/$', 'auth.views.show_profile', name='profile')
When i access it from the main page with url: e.g /1001/profile/ it loads fine but when I try to access it from another subpage with url: e.g /1001/forms/profile/ i get the error: The current URL, /1001/forms/profile/, didn't match any of these. How can i fix this?
It is because "profile" is a relative URL, and a relative URL is appended to the current URL - the resulting address is not valid across the whole site. Seems like you should use an absolute URL in your case.
At the template you can try something like:
Profile
UPDATE
To get request available in templates you have to add django.core.context_processors.request to TEMPLATE_CONTEXT_PROCESSORS. I'm not sure if it is added by default.
You must have to add your second subpage url in urls like you did for /1001/profile/
url(r'^(?P<user_id>\d+)/form/profile/$', 'auth.views.show_profile', name='profile_form')
and also correct your code as #Paulo mentioned or you can also do it through reverse url.
Profile