I am new to rails. I have developed an application on rails recently. The application is pretty big and it's running fine. Currently i have url like this.
http://192.168.99.220/user/13/domainUsers
I want it to be like the below one (without any id)
http://192.168.99.220/user/domainUsers
My routes are like this.
match 'user/:id/domainUsers', :to => 'domains#manageDomain_2', :as => :manageDomain2
I have tried to rewrite the url using "to_param". As my application is too big and it has lots of functionalities, i am using parameters other than the "id" to find users informations frequently, so i am not being able to use the "to_param" method. Is there any other way to hide "id" from url.
Please help
Thanks in advance.
The easiest way to do this is with a gem called friendly_id.
Here is a tutorial that explains it quite well:
http://railscasts.com/episodes/314-pretty-urls-with-friendlyid
Related
So I am ruby/rails novice and have been looking at: https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline This is the link to the twitter docs for using the 1.1 api. I am clear that I need a twitter app which I have setup, and that I need to set global vars for the key and secret, but then what.
Should I create a controller method in my Pages_controller (the model for static pages)?
How should I code this method to grab my global vars and then create an instance var that includes my tweets?
thanks for the help
You say no gem but much easier if you just use the gem Twitter, put the config in a module if you want. It will look something like this
twitter_client = Twitter::Client.new(
:consumer_key => ENV["CONSUMER_KEY"],
:consumer_secret => ENV["CONSUMER_SECRET"],
:oauth_token => ENV["OAUTH_TOKEN"],
:oauth_token_secret => ENV["OAUTH_SECRET"],
)
In your controller you can then retrieve the tweets with something like
twitter_client.user_timeline('twitter_handle').map(&:attrs)
Any further help just ask
I'm wanting to use a short url similar to a short url from bit.ly. Currently i have resources :clips instead of accessing clips/:id in my route i want to access it from my url_hash field on my Clips model. Am I going to have to manually list the routes to do this?
I would use the slugged gem:
https://github.com/Sutto/slugged
or (more popular):, friendly_id
https://github.com/norman/friendly_id
I have an existing website which I am attempting to port over to rails (3.2.7) and need to maintain the current urls.
Current website has urls like this:
http://example.com/Joe
http://example.com/Bob
Using rails the closest I have come is using the friendly_id gem and get this:
http://example.com/users/Joe
http://example.com/users/Bob
Every example I find seems to include the controller name in the url. How can I generate urls like the existing website?
Assuming you have :resources :users somewhere in your routes.rb you can put the next route definition in file:
match '/:name' => "users#show"
This way the url /Joe will direct to UsersController show action, populating params[:name] with the string 'Joe'.
You can find all the configuration steps needed here, 'Removing the controller names from URLs'
I'm working with a Rails 3 Routes file and the resource mapping looks like this:
resources :projects do
new do
post :add_test_phase
post :add_client
post :refresh_form
end
I've read the Routes Guide for Rails 3 but find no mention of this. I know what "member" or "collection" add but am stumped by this new tag. Does it mean perform the mentioned posts when a new project is created?
It works just like the post do block does. It's just for creating a bunch of new routes. Your above example would give you add_test_phase_new_project_path mapped to projects#add_test_phase, add_client_new_project_path mapped to projects#add_client, refresh_form_new_project_path mapped to projects#refresh_form. The urls would be /projects/new/add_test_phase, /projects/new/add_client and /projects/new/refresh_form. Although, honestly, I don't really see a good use case for this.
I trying to prevent url hacking, I passing an id to the url that the forms need, it works fine but if the user changes that value on the url it will send values to the wrong table.
<%= link_to '+ New Event',
{:controller =>'events', :action =>
'new', :company_id => company.id} %>
On the php world I used to encrypt that id ...how can I do this on rails3 or is there a better way ??
needless to say I sort of new to rails and I know a little bit of php
any help or suggestions will be greatly appreciated.
Even though this is an older question, it's a very worthwhile question. It is absolutely worthwhile to conceal the ID in the URL for, among other things, prevention of information disclosure.
For example, an application has a robust security model allowing users to only view resources to which they have rights. However, why should a user be able to look at the value of the ID in the URL and use it to deduce how many resources there are or, as the original questioner suggests, start trying to poke around with forced browsing.
The solution to this in rails turns out to be pretty simple. What I find works best is overriding to_param in the models, usually via a module in the lib directory and a before_filter in the application controller that decrypts the IDs.
For a walkthrough, have a look at this: http://www.youtube.com/watch?v=UW_s9ejrCsI
Rather than trying to encrypt or hide your company.id value, ask yourself what exactly it is that you want to prevent users from doing.
If you just want to prevent users from creating events associated with non-existant companies (by setting the id to a really high value for instance), then a simple
validates_presence_of :company
On the Event model would be fine.
If you only want users to be able to create events associated with companies that they work for, or have access for in some way, then you should create custom validations to verify that.
F