Rails routing to external site from model column through controller - ruby-on-rails-5

I'm having an issue in my controller. I'm making url objects with original_url short_url and sanitized_url. I can create and save the links just fine. The issue i'm having is when following the short link back to mysite.com/short_url it needs to go through the controller show and grab the sanitized url and redirect to that external site.
Can someone help me figure out what's wrong with this code?
I'm getting undefined method 'sanitized_url'
urls_controller.rb - show
short = params[:short_url]
#url = Url.where("short_url = ? ", short)
redirect_to #url.sanitized_url
My routes.
root to: 'urls#index'
get "/:short_url", to: "urls#show"
get "shortened/:short_url", to: "urls#shortened", as: :shortened
resources :urls
Thank you

undefined method 'sanitized_url'
where returns AR collection. You need to apply sanitized_url on an instance
Below should work
short = params[:short_url]
#url = Url.where("short_url = ? ", short).first
redirect_to #url.sanitized_url

Related

Cannot Extract Params from URL

Good Afternoon,
I have a external GET request hitting a controller action but i cannot retrieve the params.
the URL looks like:
https://yourway.local/strava/webstatusrep?hub.verify_token=STRAVA&hub.challenge=15f7d1a91c1f40f8a748fd134752feb3&hub.mode=subscribe
i have tried using
$param1 = $request->getQueryParam('hub.challenge');
or
$param = ArrayHelper::getValue(Yii::$app->request->get(), 'hub.challenge');
But also in Xdebug i can see they are not sitting in the “queryparams” section.
Just replace . with _ in param name when calling Yii2 api:
$paramValue = Yii::$app->request->get("hub_challenge");

Ruby on Rails undefined local variable or method

I'm having a problem with redirecting my website to a different page, if the person is younger then a certain age. Either, I need more coffee or I've lost it, I can't seem to see what I did wrong. Thanks so much.
app/models/student.rb
Im trying to sent people to a different site with redirect
private
def must_be_over_13
if birthday && birthday > 13.years.ago
redirect_to under_13_landing_page_path ``
end
end
I defined under_13_landing_page
controller/student controller
def under_13_landing_page
end
views/students
I made the redirect page
under_13_landing_page.html
config/routes.rb
I told it to redirect it to the landing page
get "games_for_kids", to: "students#under_13_landing_page", as: 'under_13_landing_page'
THE ERROR**undefined local variable or method `under_13_landing_page_path' for #**
rendering and redirection are Controller's responsibility NOT Model's. You should not be redirecting in the Model app/models/student.rb. This is the reason of the code failure as Model doesn't have access to the route helper methods such as under_13_landing_page_path in your case.
I would suggest you to return a boolean value from Student#must_be_over_13 method which you can then check in the concerned controller and redirect appropriately.

bullet proof way to find pages in Refinery Cms

i can find a page I'm looking for just fine, like this:
#my_page = ::Refinery::Page.find('foo')
so then i can say <% link_to #my_page.url %>
i then try and protect against the case where that page is deleted, like so:
if ::Refinery::Page.find('foo').present?
#my_page = ::Refinery::Page.find('foo')
else
#my_page = nil
end
i've actually tries several ways of doing this, with exists?, with nil?, etc. the above is the most basic.
So I then go and delete my page and I get a record not found error.
Couldn't find Refinery::Page with id=foo
Shouldn't the present? method protect against that error? How should I be doing that?
When you do
#my_page = ::Refinery::Page.find('foo')
it tries to find page by slug first and if it doesn't find it then it tries to find it by id.
If you don't deal with localization you can do
#my_page = ::Refinery::Page.find_by_slug('foo')
which will return page if it finds it or nil if it doesn't.
With localization it get's complicated but this should do the trick
if ::Refinery::Page::Translation.find_by_slug('foo').present?
#my_page = ::Refinery::Page::Translation.find_by_slug('foo').refinery_page
end
How about following the principles of EAFP?
begin
#my_page = Refinery::Page.find('foo')
rescue ActiveRecord::RecordNotFound
#my_page = nil
end

How do I do a redirection in routes.rb passing on the query string

I had a functioning redirect in my routes.rb like so;
match "/invoices" => redirect("/dashboard")
I now want to add a query string to this so that, e.g.,
/invoices?show=overdue
will be redirected to
/dashboard?show=overdue
I've tried several things. The closest I have got is;
match "/invoices?:string" => redirect("/dashboard?%{string}")
which gives me the correct output but with the original URL still displayed in the browser.
I'm sure I'm missing something pretty simple, but I can't see what.
You can use request object in this case:
match "/invoices" => redirect{ |p, request| "/dashboard?#{request.query_string}" }
The simplest way to do this (at least in Rails 4) is do use the options mode for the redirect call..
get '/invoices' => redirect(path: '/dashboard')
This will ONLY change the path component and leave the query parameters alone.
While the accepted answer works perfectly, it is not quite suitable for keeping things DRY — there is a lot of duplicate code once you need to redirect more than one route.
In this case, a custom redirector is an elegant approach:
class QueryRedirector
def call(params, request)
uri = URI.parse(request.original_url)
if uri.query
"#{#destination}?#{uri.query}"
else
#destination
end
end
def initialize(destination)
#destination = destination
end
end
Now you can provide the redirect method with a new instance of this class:
get "/invoices", to: redirect(QueryRedirector.new("/dashboard"))
I have a written an article with a more detailed explanation.

How do I send multiple params in my rails3 form controller

I'm trying to send a number of virtual attributes from my form but keep running in to an error about not being able to convert symbol to integer.
My controller's got this in it:
#user = User.generate_batch(params[:user][:username_l][:quantity])
And in my model:
def self.generate_batch(username_l, quantity)
What am I doing wrong and where can I read up on this??
S
For one thing, generate_batch has 2 arguments and you provide only one, and in the wrong place I believe.
For future reference, when passing multiple params, I needed to do the following. I hope it helps someone else.
#user = User.generate_batch(params[:user][:username_l],params[:user][:quantity])