Rails 3.1 routing error, Named parameter not working - ruby-on-rails-3.1

I have a route setup for downloading a document which I need a named parameter for the document name so I get the doc name on the download document. I'm also passing a few unnamed parameters to identify the document.
Looks like this:
match "download/:name", :to => 'documents#download', :as => "doc_download"
And I have my link setup like this:
<%= link_to doc.pdf_name, doc_download_url(doc.pdf_name,
:prefix => doc.doc_prefix,:num => doc.doc_num, :change => doc.doc_change) %>
When I run the page I get the following error:
No route matches {:controller=>"documents", :action=>"download",
:prefix=>"D", :num=>"002", :change=>0, :name=>"sdr_vor_000.pdf"}
The weird thing is the route shows up in my rake routes:
doc_download /download/:name(.:format) {:controller=>"documents", :action=>"dow
nload"}
Ideas?
Also if I remove the name parameter or make it optional, it works, but doesn't stick the name into the url. It just gets tacked onto the query string.

Your name parameter contains a period - by default, the part after the period is interpreted as the format, not part of the name parameter. To allow periods in the filename, amend your route like this (tweak the regex to your needs):
match "download/:name", :to => 'documents#download',
:constraints => { :name => /[a-z0-9\.]+/i }, :as => "doc_download"

Related

Ruby Grape Api multiple routes with single method [duplicate]

I want to catch 2 similar route in one action block. In Rails5 I can do that easily. I first declare this:
get ':folder/:file' => 'get#index', :file => /.*/, :folder => /.*/
get ':file' => 'get#index', :file => /.*/
This allows me to catch :folder as much as folder can be like a/b/c/d... and :file at end one last filename. Second one also allow me to only catch filenames. And both routes target to same action.
However, In Grape because it is declared as blocks rather than route to method definitions, I have to write same block twice...
Is there any way to catch both /as/many/folder/and/file.ext and just /file.ext in one route parameter? I tried optional params, requirements. None of them worked.
The reason behind I use :folder/:file (twice regexp) is i can grab :folder param and :file param separately without manually splitting them.
get ':folder/:file', requirements: { file: /.*/, folder: /.*/ } do
# params[:folder] and params[:file]
end
get ':file', requirements: { file: /.*/ } do
# params[:file]. [:folder is empty.]
end
^^ I want to make them one single route. If folder exists (nested) then it will grab in folder param otherwise folder will be nil.
Ok. I've found the answer by trying and looking to refdocs.
get '(:folder/):file', requirements: { folder: /.*/, file: /.*/ } do
This works as expected.
Example:
desc 'Create transaction'
params do
requires :id, type: String
requires :from_, type: String
end
post ['/:id/addresses/:from_/transactions', '/:id/transactions'] do
end
Routes:
/api/v1/wallets/:id/addresses/:from_/transactions
/api/v1/wallets/:id/transactions

How do you establish the parent of an object as an object in page object?

In a page object file:
class ThisPage
include PageObject
I can establish an object like this:
div(:user_dialog, :class => 'ud_dialog')
However, in the domain of the website, there are many windows with :class => 'ud_dialog' that popup in various workflows.
I can get to the object in binding.pry like this:
on(ThisPage).div_elements(:text => 'Are you sure you want to do this action?').first.parent.html
How can I establish the window like this in the page file?
i.e. is there some syntax like this:
div(:user_dialog, parent(:text => 'Are you sure you want to do this action?'))
For complex locators, such as locating the parent element, you can use a block to get the element.
If you define the div in your page object as:
div(:user_dialog){
div_elements(:text => 'Are you sure you want to do this action?').first.parent
}
Then your page can do:
on(ThisPage).user_dialog_element.html
Note that since you want the first matching div, you could simplify it to:
div(:user_dialog){
div_element(:text => 'Are you sure you want to do this action?').parent
}
It might also be possible to be more direct by using multiple locators (depending on what your html is). You could locate the div that has the ud_dialog class and contains the specified text (a partial match since there is likely other text):
div(:user_dialog, :class => 'ud_dialog',
:text => /Are you sure you want to do this action?/)

redirect with saving url params?

some time ago forum was created in public directory of a rails app. then forum was moved to a sub-domain.
I've created a redirect for 'domain.com/forum' => 'forum.domain.com by editing routes & creating redirect action.
My question is: how may i preserve url params (ex. 'domain.com/forum?thread1&=1' => 'forum.domain.com?thread1=1' & etc.)
My code as follows:
routes.rb:
map.forum '/forum', :controller => "application",
:action => "redirect_to_forum"
application_controller.rb
def redirect_to_forum
redirect_to "http://forum.domain.com"
end
You can try with getting request url in a hash :-> and then try to preserve your parameters,
on the top of the page use
require 'cgi'
and then get the url wherever you want to get it and use it. After getting parameters in hash u can use them to reconstruct your new url.
parameters = CGI::parse(request.url)
parameter will contain the hash of your all parameters.

Instantiating Devise user models manually using contents of params hash

I'm losing my mind trying to figure this out:
There's a registration module which side-steps the main devise registrations controller, and I have to manually instantiate the Devise-generated user model using User.new(...). I'm using a form_tag to submit all of the necessary parameters necessary for User.new(). Here's a truncated version of my code:
def registration_three
#user = User.new(:email => params[:email], :password => params[:password], :password_confirmation => params[:password_confirmation])
#user.save
#user = User.new(:email => 'patlopez#yahoo.com', :password => 'qwertyui', :password_confirmation => 'qwertyui')
#user.save
end
The second #user.save works, but the first one doesn't. params[:email], params[:password], and params[:password_confirmation] certainly exist, according to the print statements in stdout.
Anyone have any ideas?
EDIT:
So I tried changing all of the single-quote strings in the second User.new(...) to double-quote strings like the ones stored in the params hash...and the second instantiation failed as well. So I'm fairly sure that the issue involves double- vs single-quote strings. Anyone know how to convert double-quote strings to single-quote ones? Thanks in advance.
Store your params in variable first then use them in query to avoid quote errors

Rails Routes Path Trailing Slashes

Could someone please tell me why this
<%= destroy_password_url #user.password_reset_token %>
generates
http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw
routes.rb
get 'api/destroy_password' => 'services#destroy_password', :as => 'destroy_password'
I'm just following http://railscasts.com/episodes/274-remember-me-reset-password?view=asciicast I don't even know why it's adding a DOT. Could someone please help me out?
In your route file, you're not saying that you accept a parameter. You should probably rewrite it like this:
get 'api/destroy_password/:id' => 'services#destroy_password', :as => 'destroy_password'

Resources