How do you add parameters to the current URL? - ruby-on-rails-4

I want to add a language modifier via query, by adding "?lang=xx" to the current URL.
I'm currently using something like this:
=link_to request.request_parameters.slice(:lang).merge(lang: 'en')
This is fine, except it adds "welcome/index" or the correspondent route when I use customized routes, which is not great for sharing links.
I also tried this:
=link_to "#{request.path}/?lang=en"
Simple, it works, but doesn't allow me to remove unwanted parameters.
I read somewhere that there was one of these that was supposed to not include "welcome/index", I've already tried request.query_parameters and request.request_parameters, both include 'welcome/index' when I do something while on my website's root.
I could add conditionals and remove the controller and action when I'm in a customized route, but I think that's unnecessary work.
Is there a clean way to do this without overengineering a solution?
I'm currently on Rails 4.2.5 and thinking on upgrading to 5.0.

Guess I had to overengineer it. This is the simplest implementation I could think of:
Slim:
=link_to_if request.path == '/', 'Home', root_path(lang: :en) do
=link_to 'Not Home', request.request_parameters.slice(:lang).merge(lang: 'en')
I know it's two lines, I just hate adding more code than it'd seem necessary.

Related

Ember.js routing with parameters

I just played with ember routing example. It looks quite interesting. Especially if you are going to build all your application on Ember framework.
But parameters in url follows after '#'. That means you can't copy and send a link to someone if client has to login with postback (if only to set a cookie with login parameters). Is there a better option - maybe use '?' instead of '#'?
You may also have a look at Ember.Router.
There are two good start points # https://gist.github.com/2679013 and https://gist.github.com/2728699
A lot of fixes have been made the last couple of days.
EDIT
A brand new guide is now available # https://emberjs-staging-new.herokuapp.com/guides/outlets#toc_the-router
Here is a full example courtesy of https://github.com/jbrown
http://jsfiddle.net/justinbrown/C7LrM/10/

In ExpressionEngine 2, is it possible to use the same 'page' template for multiple pages?

Let me clarify: I am building a relatively simple brochure site that has multiple sections that I would like to use the same 'template'. For example: http://example.com/section1 & http://example.com/section2. I would also like these two sections to use the same 'view' template for their individual entry pages, for example on http://example.com/section1/item1 or http://example.com/section2/item2/.
Is this possible? I am very new to EE & have been reading about lots of ways to change url structure etc but nothing quite like what I'm asking. I am also aware that my solution could be achieved by creating two template groups with identical index templates with them but that seems rather silly.
P.S. I know my channel settings might be relevant so please let me know what they should be in order to answer this.
Many thanks
Zander
Yes, this is possible, and you've got a few options.
The first is to use Structure. Structure allows you to use the same page templates across multiple pages (ie http://example.com/section1 and http://example.com/section2). This is probably your best bet.
The second option is to use the Pages Module that comes with ExpressionEngine.
The third option is to make your index template a simple 'router'. It would go something like this:
{if segment_2 == 'item1'}
{embed="template_group/template_name" entry_id="4"}
{if:elseif segment_2 == 'item2'}
{embed="template_group/template_name" entry_id="3"}
{if:elseif segment_1 == 'section2'}
{embed="template_group/template_name" entry_id="2"}
{if:elseif segment_1 == 'section1'}
{embed="template_group/template_name" entry_id="1"}
{/if}
This will look at the URL segments and then embed the same template for each, and pass it the entry ID which you can use in your {exp:channel:entries} tag to display the content from the appropriate entry.
Yes, this is possible. Checkout the Pages module.
The Pages module will allow you select the URL as well as the template. When installed the Pages module will add a new 'pages' tab to the edit screen. Make sure you are not setting the dynamic parameter to 'no' in your {exp:channel:entries} tag pair.
There are other solutions to this problem, but this is the most straight forward.

django internalization in urls? how to make urls like this: "en/articles" and "pt/artigos"...?

Hy!
I would need to make urls based on language.
Example:
If I had the language english and subcategory named "articles" then the url might be like this:
/en/articles/...
If the language were portuguese and subcategory already translated is "artigos" then the url will be:
/pt/artigos/...
How can I do it?
I must use the urls.py or some monkeypatches?
thanks
This features is already existing in the yet-to-be released version 1.4. You can read about it in the release note.
If your really need this feature, and no existing app feets your needs, you can still try to apply the corresponding patch yourself.
Django LocaleURL is a piece of middleware that does exactly this. The documentation can be found in the source, or online.
Edit: I read over the fact that you want to translate the url itself... I'm not aware of any piece of code that provides this. Perhaps you could extend the LocalURL middleware to take care of the translations for you. Say you have a regex match like (?P<articles>\w+), you could in the middleware determine which view you want to use. Something like the following mapping perhaps?
if article_slug in ['articles', 'artigos', 'article']:
article(request) # Call the article view
I've been using transurlvania with great success, it does exactly what you need and more, however i see that in the next Django release django-i18nurls will be included in django core so perhaps it would be better to learn that

Is there something between middleware and view in Django so that I can plug my code into?

Is there something between middleware and view so that I can plug my code or do I have to subclass something from Django to provide this functionality?
Let me first explain why I need this, maybe there is a better solution that you can suggest. I want to restrict some of my url's based on some configuration. And,
- I want this configuration to be part of url configuration
- According to the config provided, I want to redirect, etc to some other view.
What I mean by 'part of url configuration' is something like the following.
url(r'^admin/blah/blah$', do_something, name='admin-blah-blah', {'security_level': 'very_secure', 'auth_method' : 'oauth', 'auth_url', 'http://www.foo.com'})
It seems like it is something that should be done by middlewares, but I don't want to do it with middlewares for 2 reasons.
- I don't want to maintain a separate config.
- I don't want to do regex matching for url patterns one more time, url resolver is already doing that
So if I can just find a way to plug some functionality just before view and can reach the configuration provided, it solves my problem.
Sounds like you could do this with a decorator on your views:
#restrict_url(security_level='very_secure', auth_method='oauth',
auth_url= 'http://www.foo.com')
def my_view(request):
... etc ...
You can get some ideas of how to write the restrict_url decorator by looking at the ones provided in django.contrib.auth.decorators.

How do I add a prefix to all urls and generically parse that as a kwarg

Let's say I have a site where all urls are username specific.
For example /username1/points/list is a list of that user's points.
How do I grab the /username1/ portion of the url from all urls and add it as a kwarg for all views?
Alternatively, it would be great to grab the /username1/ portion and append that to the request as request.view_user.
You might consider attacking this with middlware. Specifically using process_request. This is called before the urlresolver is called and you can do pretty much anything to the request (request.path in this case) you want to. You might strip out the username and store it in the request object. Specifics depend (obviously) on the conditions under which you do/do not want to remove the first path component.
Updated for comment:
Whichever way you go about it, when you call reverse() you have to give it the additional context info -- it can't just automagically figure it out for itself. Django doesn't play any man-behind-the-curtains games -- everything is straight Python and there isn't any global state floating around just off stage. I think this is a Good Thing™.