Possible to dynamically change the name of a symbol in a partial? - ruby-on-rails

I'm curious if there's a way to change the name of a symbol in a partial based off the parameters that's passed to it. For example, it's possible to do something like this
in a partial:
f.label("#{parameter}")
where parameter was passed into the partial when it was rendered. Now lets say instead we have something like
blog_path(domestic_or_international: "domestic")
would it be possible to use a similar method to change the symbol domesitc_or_international like I did in the first example? Thanks a bunch!

you can use the old hash syntax to do this.
blog_path(:"static_text_#{parameter}" => "domestic")

Related

Is there a list of reserved object names for Rails partials?

If I have a partial named _string.erb and try to render it with something like:
= render partial: "string", object: "Some string i want to show in some manner"
And then try to reference in my partial with the name string it throws the error
undefined local variable or method 'string'
The only thing I can think of is that the object name is reserved. If something else is going on I would love to hear about it.
Just for clarity, everything works fine for a partial with another name. eg in the partial dog rendered with the following code
= render partial: "dog", object: "Some string i want to show in some manner"
I can reference the variable dog without any issues. I am also NOT using #string.
I realize I may be able to find this out by digging into the actual source code (which I intend to do after posting this), but if anyone has an idea what is going I would love to hear it. If I find a solution I will post it.
EDIT - edited to add the exact error being thrown due to questions about it in the comments.
EDIT - I tried to simplify my example when writing this and ultimately ended up proving that string isn't a reserved keyword, and my code was erroring because the value I was passing was nil in the db.
There is no official definitive list of reserved words. The list found at https://reservedwords.herokuapp.com/ is the best I've come accross. String is included in the list, so very well may be the cause of your issue.
I am 99% sure the source of my error was that my string I was passing in was nil. I expected this to return nil to the partial under that variable name string, but that doesn't appear to be the case and I just missed it.

URL Encode string for Href ASP.NET MVC / Razor

I'm trying to build an Href using Razor
The string is going to end up looking like this:
https://www.notmysite/controller/action?order_ID=xxxxxxx&hashComparator=iFxp3%2BszAMaEB%2FnHCtRr9Ulhv0DumtyDumCik4gKypJqi0BdOGXXsr9QDkfefAsLaR1Xy%2BrX9VcuzP1pF2k6dL%2F92UxphzTKqNAZP2SSZGWtvyO5az%2F9JvDY%2Bsq5TBQo7%2FYAAMIU4QxiSX1SBKk8SUNECW3ZmKM%3D
In my model I have the order id and the hash string
As the route is not a part of my site I don't believe I can use the default methods like #Url.Action
and therefore can't use protocol: Request.Url.Scheme
like I've used elsewhere.
So at present I'm trying to figure out how to create this using string functions
I've tried
Url.Encode
Url.EscapeDataString
Html.Encode
but am getting no where fast:
Click Here to be transferred
The output text always has plusses and equals in them and doesn't work.
Which combination do I need?!
I've figured out a way of doing it:
#{
var url = string.Format(
"https://www.notmysite.co.uk/controller/action?order_ID={0}&hashComparator={1}",
#Uri.EscapeDataString(Model.bookingNumber.ToString()),
#Uri.EscapeDataString(Model.hashCode));
}
<p>Click Here to be transferred</p>
Edit 2015 - As mentioned by Jerads post - The solution is to only encode the query string elements and not the whole URL - which is what the above does.
This was the first link that came up for this issue for me. The answers didn't work for me though because I am using core, I think. So wanted to add this in.
System.Net.WebUtility.UrlEncode(MyVariableName)
If Url.Encode doesn't work try the above. Also as stated before don't use this on the entire URL string, just use it for the individual querystring variables. Otherwise there is a good chance your URL wont work.
The problem is that you're trying to encode the whole URL. The only pieces you want to encode are the querystring values, and you can just use Url.Encode() for this.
You don't want to encode the address, the querystring params, or the ? and & delimiters, otherwise you'll end up with an address the browser can't parse.
Ultimately, it would look something like this:
Click Here to be transferred
The easier method is to use #Html.Raw(Model.SomethingUrl)

How to map an URL having querystring parameters in urlmapping.groovy?

I am new to Grails/Groovy. Please bear with me if this is a silly question.
I am trying to map an URL having querystring parameters in urlmapping.groovy, something like,
"/rest/employees?empno=123&dept=delivery"(controller:"employees", action="emp")
The values 123 and delivery are dynamic values. In other words, this could be anything that the user can use.
Apparently this syntax is not correct. What is the right way to achieve this?
Just leave /rest/employees"(controller:"employees", action="emp") and you'll get your query parameters as params.empno and params.dept in your action
#splix answer is correct. If you want to use a restfull url, you could do something like
"/rest/employees/$empno/$dept"
instead. Otherwise just leave out the part after "?" as said. In your controller you will still get params.empno and params.dept
Read more here

Grails g:paginate tag and custom URL

I am trying to use g:paginate in a shared template where depending on the controller, url changes e.g.
For my homepage url should be : mydomain[DOT]com/news/recent/(1..n)
For search Page: www[DOT]mydomain[DOT]com/search/query/"ipad apps"/filter/this month
and my g:paginate looks like this:
g:paginate controller=${customeController} action=${customAction} total:${total}
For the first case, I was able to provide controller as 'news' and action as 'recent' and mapped url /news/recent/$offset to my controller.
But for the search page, I am not able to achieve what I want to do. I have a URL mapping defined as /search/$filter**(controller:"search",action:"fetch")
$filter can be /query/"ipad apps"/filter/thismonth/filter/something/filter/somethingelse.
I want to be able to show the url as above rather than
?query="ipad apps"&filter=thismonth&filter=something&filter=somethingelse.
I believe I can pass all the parameters in params attribute of g:paginate but that will not give me pretty URL.
What would be the best way to achieve this? Please feel free to ask questions If i missed anything.Thanks in advance.
Ok. figured this out. Hope this helps someone.
Set ${customAction} ='' and this query string 'filter/thismonth/filter/something/filter/somethingelse' was passed with 'params' alltribute.
Thanks

<%= debug(controller) %> produces unreadable output

In one of my templates I want to take a closer look at the controller object using the debug() helper.
It is said to produce YAML suitable for HTML output.
Running <%= debug(controller) %> does, however, not produce anything I call readable.
It begins with:
#<ClubsController:0x104467378 #_request=#<ActionController::Request:0x104467648 #accepts=[#<Mime::Type:0x101f50f30 #synonyms=["application/xhtml+xml"],
and goes on like that...
Any ideas on how to get some info about the object?
The debug helper is primarily intended to be used with objects that provide an implementation of to_yaml (such as ActiveRecord models). If the object passed doesn't respond to to_yaml (as is the case with passing a controller object in your example) then debug gives you the result from calling inspect on that object.
I saw your comment #thenduks answer.
Actually there is a method:
controller.controller_name
that returns the name of the controller.
What's not readable about that? It's the standard ruby inspection syntax... If you really need something 'more readable' then you'll have to write a method on your controller (temporarily, most likely) that simply exposes it's instance variables in another format.
Perhaps it would help if you noted what you want to see in the controller instance.

Resources