Magento: Building a store URL in CMS-syntax, using a variable - templates

I am building an e-mail template. I want to build a URL like this:
http://store.com/path/to/page?shipmentid=123
This code builds the correct URL:
{{store url='path/to/page' _query_shipmentid=123}}
But the 123 part should be dynamic. I need it to pull from this variable:
{{var shipment.id}}
Is this even possible? I'm looking for something like this:
{{store url='path/to/page' _query_shipmentid=shipment.id}}

Use $ prefix to let Magento know that it is variable. This code should work:
{{store url='path/to/page' _query_shipmentid=$shipment.getId()}}

Related

How to use variable at start of django url to return to view?

I am trying to pass the first part of a django url to a view, so I can filter my results by the term in the url.
Looking at the documentation, it seems quite straightforward.
However, I have the following urls.py
url('<colcat>/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('<colcat>/', views.collection_view, name='collection_view'),
In this case, I want to be able to go to /living and have living be passed to my view so that I can use it to filter by.
When trying this however, no matter what url I put it isn't being matched, and I get an error saying the address I put in could not be matched to any urls.
What am I missing?
<colcat> is not a valid regex. You need to use the same format as you have for name.
url('(?P<colcat>[\w\-]+)/collection/(?P<name>[\w\-]+)$', views.collection_detail, name='collection_detail'),
url('(?P<colcat>[\w\-]+)/$', views.collection_view, name='collection_view'),
Alternatively, use the new path form which will be much simpler:
path('<str:colcat>/collection/<str:name>', views.collection_detail, name='collection_detail'),
path('<str:colcat>/', views.collection_view, name='collection_view'),

Ruby convert Url parameters to array

I have this url encoded:
Started PUT "/path/thing/9812/close?status=close&shutdown_on=2018-12-05%2010%3A08%3A06&affected_external_id=15027&fqdns%5B0%5D=150.212.3.249"
which decoded is this:
"/path/thing/9812/close?status=close&shutdown_on=2018-12-05 10:08:06&affected_external_id=15027&fqdns[0]=150.212.3.249"
I get this parameters:
Parameters: {"status"=>"close", "shutdown_on"=>"2018-12-05 10:08:06", "affected_external_id"=>"15027", "fqdns"=>{"0"=>"150.212.3.249"}, "id"=>"9812"}
How can get fqdn as a array? on Rails 4
You should do the following:
params[:fqdns].to_a
Doing this, will produce the following:
{['0', '150.212.3.249', ...]}
If you wnat only the values, may you can try:
params[:fqdns].values
Doing this, will give you the following:
['150.212.3.249', ...]
But for this, you have to do it inside a ruby class, i strongly recommends you to do it inside your controller. Hope i can help.
UPDATE
After a recommends, you can do it with strong parameters, permiting the param fqdns as a hash (because you route receiving a hash):
def resource_params
params.permit(....., fqdns: {})
end
After this, you already have to execute the solutions above to get fqsnd as a array

Django missed slash in url

I created a url like 'api/personal/'. Everything went right when I did local test using './manage.py runserver'. But when I used factoryboy to create a client and try to get the detail by 'self.user_client.get('api/personal/')', the response showed 404 NOTFOUND because the url had changed to apipersonal/. Does anyone know why did it happen?
Use named urls for avoiding this kind of confusions. Define the url like this:
path('api/personal/', your_view, name='api_personal') # added keyword argument name
and use it in the tests with reverse like this:
self.client.get(reverse('api_personal'))

URL not resolving correctly in Django URL

I just tried the following in my AJAX update:
[Server]/secTypes/Update
This maps to the following url in URLS.py:
url(r'^secTypes/Update/', equity.views.updateSecTypes, name='updateSecTypes'),
This doesn't resolve to the following function in my view.
But when I change the URL expression to:
url(r'^su/', equity.views.updateSecTypes, name='updateSecTypes')
It works fine.
What in the URL resolver is not getting accurately mapped? Is it the forward slash?
I think it has to do with something related to the regex so if someone understands this better can help me that would be appreciated.
From the url patterns in your comments, it looks like you had another matching pattern before the one in your question.
There are two simple solution for this.
Move that first pattern down. Change this:
url(r'^secTypes/', equity.views.getSecTypes, name='getSecTypes'),
url(r'^secTypesAll/', equity.views.getSecTypesAll, name='getSecTypesAll'),
url(r'^secTypes/Update/', equity.views.updateSecTypes, name='updateSecTypes'),
url(r'^secTypes/Delete/', equity.views.deleteSecTypes, name='deleteSecTypes'),
url(r'^secTypes/Create/', equity.views.createSecTypes, name='createSecTypes'),
to this:
url(r'^secTypesAll/', equity.views.getSecTypesAll, name='getSecTypesAll'),
url(r'^secTypes/Update/', equity.views.updateSecTypes, name='updateSecTypes'),
url(r'^secTypes/Delete/', equity.views.deleteSecTypes, name='deleteSecTypes'),
url(r'^secTypes/Create/', equity.views.createSecTypes, name='createSecTypes'),
url(r'^secTypes/', equity.views.getSecTypes, name='getSecTypes'),
The order matters when resolving URL patterns and if an earlier one matches, the following ones are not processed.
Both r'^secTypes/' and r'^secTypes/Update/' matches the string 'secTypes/Update/' so you need to be careful to put the more specific one first and the more general one afterwards.
Update the regex to match the end of the URL string by adding a $ like this:
url(r'^secTypes/$', equity.views.getSecTypes, name='getSecTypes'),
url(r'^secTypesAll/$', equity.views.getSecTypesAll, name='getSecTypesAll'),
url(r'^secTypes/Update/$', equity.views.updateSecTypes, name='updateSecTypes'),
url(r'^secTypes/Delete/$', equity.views.deleteSecTypes, name='deleteSecTypes'),
url(r'^secTypes/Create/$', equity.views.createSecTypes, name='createSecTypes'),
This is the preferred solution since it would stop Django from matching a URL like secTypes/Update/foobar
However, if you have logic in the view that specifically uses the substring after the end of the URL pattern (i.e. foobar based on the above example), this wouldn't work.

How to insert link to controller action in Play framework 2.0 template

If I have an action Application.show(tag: String), and also have a corresponding routing entry, how can I insert a link to this action to a template without crafting the url manually?
I would like to do something like magiclink(Application.show("tag")).
syntax:
<a href='#routes.Application.show("some")'>My link with some string</a>
By analogy you can also generate urls in your controllers. ie. for redirecting after some action:
public static Result justRedirect(){
// use as String
String urlOfShow = routes.Application.index().toString().
// or pass as a redirect() arg
return redirect(routes.Application.show("some"));
}
The format for putting a URL from your routes file in your html is as follow:
#routes.NameOfYourClass.nameOfyourMethod()
So, if in your routes file you have:
GET /products controllers.Products.index()
And your Products class looks like this:
public class Products extends Controller {
public Result index() {
return ok(views.html.index.render());
}
}
Your <a> should look like this:
Products
In addition: If your method can accept parameters, then you can of course pass them in between the parenthesize of your method like this: index("Hi").
I hope this answer is more clear to understand.
The accepted answer is right, but it doesn't cover the case where the controller is in a sub-package, i.e.: controllers.applications.MyFavouriteApplication.show()
Since I had a hard time finding the answer, I'll post it here.
To put a non-scoped link into a template, the proper pattern is #controllers.{sub-packages if any}.routes.{your class}.{your method}()
So in this case it would be #controllers.applications.routes.MyFavouriteApplication.show()
IF you were using the recommended Play pattern of using #Inject to create singleton controller objects, and IF you thought the correct answer was #controllers.applications.MyFavouriteApplication.show(), you would get an error like this:
Object MyFavouriteApplication is not a member of controllers.applications. Note: class MyFavouriteApplication exists, but it has no companion object.
Given that you had already supplied the #Inject() #Singleton annotation, this would seem like a very strange error indeed. It might make you question if you were building the project correctly. Determining the true cause could cost you considerably in blood and treasure.
Ah, as simple as #{routes.Application.show("tag")}.