I am currently trying to get a handle on RoR. I am passing in two strings into my controller. One is a random hex string and the other is an email. The project is for a simple email verification on a database. The problem I am having is when I enter something like below to test my page:
http://signup.testsite.local/confirm/da2fdbb49cf32c6848b0aba0f80fb78c/bob.villa#gmailcom
All I am getting in my params hash of :email is 'bob'. I left the . between gmail and com out because that would cause the match to not work at all.
My routing match is as follows:
match "confirm/:code/:email" => "confirm#index"
Which seems simple enough for what I need. I am having a hard time trying to figure out what the deal is and really how to even search for an answer. Any help or guidance would be greatly appreciated.
match "confirm/:code/:email" => "confirm#index", :email => /.*/
Also it would be better to set get method here, I think
get "confirm/:code/:email" => "confirm#index", :email => /.*/
Your problem is that Rails is trying to interpret .villa#gmailcom as a format specification (such as .html or .json). AFAIK, the standard work around (or at least the one I use) is to add this to your route:
:requirements => { :email => /.*/ }
This tricks Rails into not trying to be clever about what :email contains.
I'm not surprised that you couldn't find anything, googling for "#" or "." doesn't do anything useful.
Related
Let's say I have this route:
get 'post/:title' => 'post#show' , :as => 'show_post'
Anyone is able to create posts, and pick their own titles.
I don't want the title to contain characters like '/', '[', ']' as it could lead to bad urls generated with the helper method show_post_path('blah'). I'm sure there are other things than those character I mentioned I should be looking out for.
I'm looking for a way to validate against this in the post model.
/ Thanks
You can use Rails parameterize method to validate the string
Ex-
result = yourstring.parameterize
here is source http://apidock.com/rails/ActiveSupport/Inflector/parameterize
I think the best way to make it will be use gem
friendly_id
I have a question regarding regex matching on a globbed route in rails 2.3. This ticket explains exactly the same problem I'm having:
Rails routes with wildcard and requirements
The problem is that I cannot get the regex for :property_types to match on anything except the first instance of a property types, and that somehow breaks the match on :location_b.
So the following only works if the property_types requirement is removed:
/for-sale/us/ny/apartment/loft/brooklyn/
map.location ":transaction/:country_code/:location_a/*property_types/:location_b",
:controller => "search",
:action => "location",
:transaction => /(for-sale|for-rent|auction|new_development)/i,
:country_code => /\w\w/i,
:location_a => /\w\w/i,
:property_types => /(apartment|loft|home|condo|multi-home|town-house)/i
Hoping someone could explain how I can get the regex for property_types working as I do not understand how it's supposed to work across a glob bed route, which is an array. The ticket I linked to seems to suggest that it's possible to have the regex match across each type, but I was unable to get anything working.
Example url:
http://localhost:3000/for-sale/us/ny/apartment/loft/brooklyn
I'm attempting to create "pretty URLs" for linking to posts in a blog. I want to maintain access to the blog entries via entries/23 and 2011/07/some-post-slug-here as I only generate a slug once an entry has been published (just in case the title of the posts changes, and, though not a strict requirement, I would prefer to be able to edit/delete posts via the entries/23 style URL. Currently, the appropriate part of what I have in my config/routes.rb:
root :to => 'entries#index'
resources :entries
match ':year/:month/:slug' => 'entries#show', :constraints => {
:year => /[0-9][0-9][0-9][0-9]/,
:month => /[0-9][0-9]/,
:slug => /[a-zA-Z0-9\-]+/
}, :as => :vanity_entry
and I use this (in my application helper) function for creating the links:
def appropriate_entry_path entry
if entry.published
vanity_entry_path entry.published_on.year.to_s, entry.published_on.month.to_s, entry.slug
else
entries_path entry
end
end
def appropriate_entry_url entry
if entry.published
vanity_entry_url entry.published_on.year.to_s, entry.published_on.month.to_s, entry.slug
else
entries_url entry
end
end
Basically, I check if the article is published (and therefore has a slug) and then use that URL/path helper, or otherwise use the default one.
However, when trying to use this, I get the following from Rails:
No route matches {:slug=>"try-this-one-on-for", :month=>"7", :controller=>"entries", :year=>"2011", :action=>"show"}
I have tried a few different solutions, including overriding to_param in my Entry model, however then I would have to create match routes for the edit/delete actions, and I would like to keep my routes.rb file as clean as possible. Ideally, I would also love to lose the appropriate_entry_path/appropriate_entry_url helper methods, but I'm not sure that this is possible?
Is there any thing I am missing regarding routing that might make this easier and/or is there any specific way of doing this that is the cleanest?
Thanks in advance.
You might want to take a look at friendly_id. It's a gem for creating seo friendly slugs :)
I found the issue with what I had been doing, the regex for :month in the route wanted two numbers, whereas I was only passing in one number. Anyways, I decided that the URLs look nicer (in my opinion) without the month padded to 2 digits, so I updated my route accordingly.
I am reading Obie Fernandez' "The Rails 3 Way", and there is a bit of it that I am not sure I understand correctly. I am new to rails, and want to make sure I understand it correctly. I have some experience with vanilla Ruby. Not much, but some.
The text in question is as follows: (regarding routing and the config/routes.rb file)
"...
By creating a route like
match 'auctions/:id' => "auction#show", :as => 'auction'
you gain the ability to use nice helper methods in situations like
link_to item.description, auction_path(item.auction)
..."
My question is, specifically what part of match 'auctions/:id' => "auction#show", :as => 'auction' creates the helper functions? (such as link_to auction and auction_path() ) Is it the :as => 'auction' part? Would any helpers be created without appending :as => 'auction'?
My confusion stems from other guides I have seen where this is omitted, and yet helpers seem to be created regardless. What specifically does rails use in match statements in the routes.rb file to create helpers? If it isn't the :as => 'auction' part, then what is the specific purpose of appending this to the match statement?
I know this seems like a super basic question, but this detail seems to get glossed over in the texts I have read thus far. Thanks in advance for any light you can shed on this.
I just tried this:
match "alfa/beta", to: 'users#new'
In this case, even without an :as => 'named_route', I got for free the following helper
alfa_beta_path
which, as expected, points to users#new.
So, it seems that helpers are also automagically generated by parsing the route's string, in case there is no :as specification.
Yes, it is the :as => 'named_route' part that creates the named route (which in turn creates the helpers). As for leaving it off, are you referring to instances of resources :something in routes.rb? The resources method generates a set of URL helpers based on the name of the resource automagically.
How would I go about manually filtering a hash using my application's parameter filter?
I imagine it'd go like this:
Rails.application.filter :password => 'pass1234'
# => {:password => '[FILTERED]'}
EDIT (clarification): I'm aware that Rails filters the params hash when writing to the logs. What I want to do is apply that same filter to a different hash at my prerogative before writing it to the logs with something like Rails.logger.info. I'm calling a remote HTTP query as a part of my application (since most of the backend operates through a remote API), and I'm logging the URL and parameters passed. I want to have the logs but also ensure that none of the sensitive params show up there.
After a few minutes of shotgunning it, I figured out this was the way to do it:
filters = Rails.application.config.filter_parameters
f = ActionDispatch::Http::ParameterFilter.new filters
f.filter :password => 'haha' # => {:password=>"[FILTERED]"}
See the config/application.rb file, towards the end there is a line:
config.filter_parameters += [:password]
This way the "password" param will not be shown in logs, but you can still access the value normally.
Edit
It seem that have misunderstood your meaning of "filter" originally. As for the clarified issue, I have no idea on how to handle it the truly Rails way.
Here is a brute force approach:
Parse the query with CGI::parse(URI.parse(my_url_address_with_params).query) to get a hash of param/values (note: values are actually stored as an array; here is the discussion).
Locate the parameters you want to filter out and replace values with literal *filtered*.
Call Rails.logger.info (or debug) directly to log.
Here is what you should dig into when relying on Rails magical classes and methods:
In Rails 3 the code that does the trick seems to live in ActionDispatch::Http (ParameterFilter in particular, method `filtered_parameters'). The documentation is available at API Dock (or, to be honest, very little documentation). You can examine the sources to get an idea of how this works.
My knowledge of Rails internals is not good enough to suggest anything else. I believe that someone with a better understanding of it might be of more help.
Building on Steven Xu's answer above, I made this initializer in my rails app:
class ActionController::Parameters
def filtered
ActionDispatch::Http::ParameterFilter.new(Rails.application.config.filter_parameters).filter(self)
end
end
Which let's me call params.filtered
[1] pry(#<LessonsController>)> params.filtered
{
"controller" => "lessons",
"action" => "search",
"locale" => "en"
}
[2] pry(#<LessonsController>)> params[:password] = "bob"
"bob"
[3] pry(#<LessonsController>)> params.filtered
{
"controller" => "lessons",
"action" => "search",
"locale" => "en",
"password" => "[FILTERED]"
}