URL manipulations in views - ruby-on-rails

I have a code which generates a URL which is like this: #{URI.escape p.url(checkout_path)}
Now I need to check a condition where, if #{URI.escape p.url(checkout_path)} generates a URL = "http://mywebsite.com" then append /trunk to the end so it must be "http://mywebsite.com/trunk" else if it already has /trunk appended from before then it should be "http://mywebsite.com".
Thus finally if http://mywebsite.com then http://mywebsite.com/trunk
elsif
http://mywebsite.com/trunk then http://mywebsite.com
But I want to know how to do it using #{URI.escape p.url(checkout_path)}

I would throw this in a helper method somewhere, but you could effectively do something like this:
URI.escape(p.url(checkout_path)) =~ /\/trunk$/ ? URI.escape(p.url(checkout_path)).gsub('/trunk', '') : "#{URI.escape(p.url(checkout_path))}/trunk"

Related

How to add file extension inside of url with ruby

This url:
url = rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id
I would like to add the extension mp3 to all file name.
In this case the file name is cancion
The id of lesson is a dynamic value.
I would like to get this url something like:
url = rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion.mp3?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id
Thanks!
You can parse the URI, edit the path, then return the value
require 'uri/http'
u = URI.parse('rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id')
u.path += ".mp3"
puts u.to_s
or use a simple regexp replace
u = 'rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id'
u.gsub('?', '.mp3?')
The second approach can be used only if you can assume the format of the input is always the same.
You can do simple gsub since this is URL and you can expect one occurrence of ? so simple do.
url.gsub!('?', '.mp3?')
Usually I would go regex here but no need from previously stated reason.

How to force the my url not to change the value of '#'

I have an URL address that I change dynamically I it goes something like this:
The dynamic part is the -> edition = Model.Edition.Usually it as an integer value and the url ends up something like that: ....&edition=1232113 . Sometimes I need it to end up like that : &edition=1232113#10_11 and I managed to pass th right value to the edition placeholder but after the reload it doesn't show the same url that I expected it is similar but it substitutes the '#' with '%23'. And it looks something like that: 1232113%2310_11 and the effect is not what I expect.
From the other side, when I type it manually : 1232113#10_11 it works.
Can you help?
If you problem is concerning that the Url.Action is converting a part of your url, you may want to use the RAW method.
#Html.Raw(Url.Action("Method","Controller", new { Id = Model.DId, dbId = Model.DbId, iconId = Model.IconId, edition = Model.Edition })

no block given yield

So executing this gives me back an error:
no block given (yield)
Well never had a deep look at blocks in ruby, which seems to be an issue in here. If you have a better solution please provider, otherwise I wanted to find a workaround for the this legacy code...
def tab_groupings
result = at_a_glance_grouping
result += rating_grouping if #domain_context.include_ratings and (controller.controller_name !='rewards_credit_cards')
result += specific_tab_groupings
result
end
def at_a_glance_grouping
result = [[:at_a_glance, yield]]
product_type = controller.controller_name == 'fairfax' ? #product_type_helper[:controller] : controller.controller_name
result[0][1].insert(0, :overall_rating) if #domain_context.include_ratings and (product_type !='rewards_credit_cards')
result
end
yield is used to execute a block that you pass to the method, and then you do something with the result of that call.
Your method at_a_glance_grouping therefore expects you to pass a block to it... which it will then execute on the following line (where you use yield)
You don't pass any blocks to at_a_glance_grouping in the first line of tab_groupings, and therefore ruby rightfully complains.
What are you trying to achieve with the yield ?
Do you really need it at all?
If not - then just remove it.
If sometimes you do pass a block to this method, then you need to check for that before calling yield eg:
result = [[:at_a_glance, yield]] if block_given?

Using coffescript in rails views doesn't work properly

I'm trying to use coffescript as views in Rails 3.2.11
I have create.js.coffee with the following lines:
is_valid = <%=#model.valid?%>
if is_valid
res = confirm("Are you sure you want to continue?")
if(res)
<%=#model.activate%>
window.location.href = "/blabla/models"
else
return
else
$('.form .field_with_errors').removeClass('field_with_errors')
jw_funcs.respond_with_error(<%=#response_invalid%>)
The problem is that the line of code <%=#model.activate%>
is executed every time. I think it depends on the fact that the erb engine runs independently from the coffee engine; If so, how can I do this ?
You really weren't expecting this coffee code to call your model method from the client's browser, were you?
Wrap #model.activate into its own controller action, which will be called by clients if the confirmation is given. Something like this:
res = confirm("Are you sure you want to continue?")
if(res)
$.ajax('/models/1234/activate', ...)
else
return

Removing a part of a URL with Ruby

Removing the query string from a URL in Ruby could be done like this:
url.split('?')[0]
Where url is the complete URL including the query string (e.g. url = http://www.domain.extension/folder?schnoo=schnok&foo=bar).
Is there a faster way to do this, i.e. without using split, but rather using Rails?
edit: The goal is to redirect from http://www.domain.extension/folder?schnoo=schnok&foo=bar to http://www.domain.extension/folder.
EDIT: I used:
url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
parsed_url = URI.parse(url)
new_url = parsed_url.scheme+"://"+parsed_url.host+parsed_url.path
Easier to read and harder to screw up if you parse and set fragment & query to nil instead of rebuilding the URL.
parsed = URI::parse("http://www.domain.extension/folder?schnoo=schnok&foo=bar#frag")
parsed.fragment = parsed.query = nil
parsed.to_s
# => "http://www.domain.extension/folder"
url = 'http://www.domain.extension/folder?schnoo=schnok&foo=bar'
u = URI.parse(url)
p = CGI.parse(u.query)
# p is now {"schnoo"=>["schnok"], "foo"=>["bar"]}
Take a look on the : how to get query string from passed url in ruby on rails
You can gain performance using Regex
'http://www.domain.extension/folder?schnoo=schnok&foo=bar'[/[^\?]+/]
#=> "http://www.domain.extension/folder"
Probably no need to split the url. When you visit this link, you are pass two parameters to back-end:
http://www.domain.extension/folder?schnoo=schnok&foo=bar
params[:schnoo]=schnok
params[:foo]=bar
Try to monitor your log and you will see them, then you can use them in controller directly.

Resources