how do I make the URL's in Ruby on Rails SEO friendly knowing a @vendor.name?
All of these solutions use find_by_name
, which would definitely require having an index on that column and require they are unique. A better solution that we have used, sacrificing a small amount of beauty, is to use prefix the vendor name with its ID. This means that you dont have to have an index on your name column and/or require uniqueness.
vendor.rb
def to_param
normalized_name = name.gsub(' ', '-').gsub(/[^a-zA-Z0-9\_\-\.]/, '')
"#{self.id}-#{normalized_name}"
end
So this would give you URLs like
/1-Acme
/19-Safeway
etc
Then in your show action you can still use
Vendor.find(params[:id])
as that method will implicitly call .to_i
on its argument, and calling to_i
on such a string will always return the numerical prefix and drop the remaining text- its all fluff at that point.
The above assumes you are using the default route of /:controller/:action/:id
, which would make your URLs look like
/vendors/show/1-Acme
But if you want them to just look
/1-Acme
Then have a route like
map.show_vendor '/:id', :controller => 'vendors', :action => 'show'
This would imply that that it would pretty much swallow alot of URLs that you probably wouldnt want it too. Take warning.
friendly_id and ActiveScaffold conflict
calling
has_friendly_id :name, :cache_column => 'cached_slug', :use_slug => true
... creates a has_many and a has one associations pointing to a slug AR model which hasn't any polymorphic belongs to association properly defined.
So basically what you need to do to solve this error is to define the reverse associations in the controller of your parent model (the one who has friendly_id stuff)
active_scaffold :products do |config|
...
config.columns[:slug].association.reverse = :product
config.columns[:slugs].association.reverse = :product
end
and it works :-)
PS : I use friendly_id as gem and ActiveScaffold VHO master branch for rails 3
What are the safe characters for making URLs?
To quote section 2.3 of RFC 3986:
Characters that are allowed in a URI, but do not have a reserved
purpose, are called unreserved. These include uppercase and lowercase
letters, decimal digits, hyphen, period, underscore, and tilde.ALPHA DIGIT "-" / "." / "_" / "~"
Note that RFC 3986 lists fewer reserved punctuation marks than the older RFC 2396.
Best strategy to use HAML template with Backbone.js
I know you already mentioned it but I would suggest using haml-js with Jammit. Simply include haml.js in your javascripts and in your assets.yml add template_function: Haml
as well as including your template files in to a package. e.g.
javascript_templates:
- app/views/**/*.jst.haml
Then in your views you can include this package (= include_javascripts :javascript_templates
) and Jammit will package any .jst.haml files in to window.JST['file/path']
. (If you view page source you should see a javascript file like <script src="/assets/javascript_templates.jst" type="text/javascript"></script>
)
To use these templates simply call one of those JSTs Jammit created. i.e.
$('div').html(JST['file/path']({ foo: 'Hello', bar: 'World' }));
And Jammit will use the Haml-js template function function to render the template.
Note: Be sure to point to the github repo of Jammit in your Gemfile to get the latest version that supports newline characters necessary for haml-js to work.
Rails 3.1 has_one nested resource: routing not generating all paths
I dont really know why it's not displayed when you do rake routes
but did you try in your code to do supplier_presentation_path(@supplier)
? It should work based on your routes.
Bundler::GemNotFound: Could not find rake-10.3.2 in any of the sources
bundle config set --local path 'vendor/cache'
generally fixes it as that is the more common problem. Basically, your bundler path configuration is messed up. See their documentation (first paragraph) for where to find those configurations and change them manually if needed.
Related Topics
Trying to Understand Use of Self.Method_Name VS. Classname.Method_Name in Ruby
Architecture for a Modular, Component-Based Sinatra Application
What Grammar Based Parser-Generator Tools Exist for Ruby
Parsing JSON Without Quoted Keys
Convert 12 Hr Time to 24 Hr Format in Ruby
How to Select the Longest String from a Ruby Array
Get Time Object at Start of Day in a Particular Time Zone
Install Rvm "Bash /Root/.Rvm/Scripts/Rvm No Such File or Directory"
Ruby on Rails - Using Concerns in Controllers
Best Way to Handle Data Attributes in Slim
How to Store Enum as String to Database in Rails
Automatically Precompile Assets Before Pushing to Heroku
Ruby Escape Argv Argument or String as Argument to Shell Command
Alias_Method and Class_Methods Don't Mix