Say I have a route users/:id, where id = 1. Pretty standard, but I would like to change this route to the following:
/account
Where any logged in user, in this case a user with an id = 1, does not see their uid as the segment key, but instead a generic 'account' URI only. How can I achieve this in a rails 3 application?
You can create a route to a standard action which functions as a users own show action. E.g.: you create an action where you get the current user's information and make it shown. The current user can be extracted from the session as usual, so no need for sending out the user id in the url. And you only need a route which redirects /account to this action.
Related
In my Rails app users have possibility to enter their own domain for their page if they want. The value of the domain name saving in the database.
For now routes are look something like this: /user/sites/3.
So, for example, user entered domain name as: "mystuff". And previous route should change to this: /mystuff
How can implement this?
Thank you.
here's an example from rails guides on how your route should look like:
get ':username', to: 'users#show', as: :user
this produces route such as /bob refering to users controller show action
what do you mean domain? you mean sub-domain or sub-url?
If you want to create sub-url for MyStuff (eg. http://www.domain.com/mystuff)
1) you need to create slug field to parameterized the text which you want to be sub-url.(or) can also use parameterized method. (eg. My Stuff => my-stuff)
2) create a route
get ":site_slug", to: 'home#site'
I have a site where you can enter the name of a business after the domain and if it exists it is displayed. E.g. www.mysite.com/BUSINESS_NAME. I do this through a route:
match ':id' => 'businesses#show', :via => [:get]
It essentially means that a new custom URL is created for each new business that signs up. At the moment each time a new business is created (create form submitted) I am redirected to something like
http://localhost:3000/businesses/42
This is expected as I use:
redirect_to #business
How am I able to redirect to the newly created business and make the URL look like
www.mysite.com/BUSINESS_NAME instead of http://localhost:3000/businesses/42
I thought about making logic in the show action that would work for either URL request but it doesn't look very nice to the end user seeing an ID - I am sure there is something in Rails to mask this sort of information and still pass the ID parameter.
Thanks in advance for any help.
Yes, you can make the url as user friendly.So user can see the simple url that dosen't use the id.
There is a gem in rails that easily solve your problem.
Friendly_id
I want to create referral links like.
www.abc.com/1234
www.abc.com/4345
Where number are the referral codes which will be unique for every user. I am sure this can be done in ruby on rails with some routes configuration. Means where the request will be routed. Which controller? which action? How to get value of unique code.
ps: launchrock is using referral links like this.
You can use this structure with route matching but you would need to have the referral codes match a specific pattern. If, for example, they matched the format of 3 letters followed by three numbers, you could put the following your routes file:
match '/:referrer_id' => 'app#index', :constraints => {:referrer_id => /[a-zA-Z]{3}[0-9]{3}/}
The reference to app#index should be changed to the controller in which you handle referrals and you can access the referrer_id through params[:referrer_id].
Certainly have a look at the link referenced in Markus' answer for suggestions on how to generate the tokens.
I have a link in my bookmarks with regard to token generation: http://blog.logeek.fr/2009/7/2/creating-small-unique-tokens-in-ruby
In your application you will need to store the individual tokens in the user table. Controller and action are up to you and for the routes you could go with something like www.abc.com/referral?123456.
routes.rb
match "/referral/:ref" => "controller#action"
access in controller with:
params[:ref]
I'm new to rails, so I'll just explain my situation to you:
I've got a User Model and a UsersController. Users log in with their email address and a password. Special users can invite other users, by typing the email address of the invitee into a form and hitting submit. The invited user then receives a mail containing a link to activate his account, by entering his password for the first time.
Here's the problem:
The "invitation" form is mapped to the create action of my UsersController atm. But what do I map the "activation" form to?
Is it possible for me to define a custom action "activate" or something that can be accessed like /users/3/activate (of course, there should be some authentication token here too...) and would activate the user with id 3?
I've found some stuff on custom actions, but I don't quite get the hang of it yet.
Thx for any help
You probably have something like this in your routes file. (If not, please post that.)
resources :users
If you want to add a custom action that acts on a single User, you can add that as a :member like this.
resources :users do
member do
get :activate
end
end
Note that using a get for something that modifies data is a bit wrong, but you're talking about hitting this from a link in an email.
As you play with routes don't forget that rake routes will show you all of the routes that you currently have available to you.
I am trying to write a "viral" pre-beta invitation application as one can see on usehipster.com or fork.ly.
Basically, the future tester:
1.) enters his email
2.) is redirected to a view (a coming_soon page)
3.) receive a link like this one : "http://localhost:3000/?referred_to=the tester's invitation id" displayed in the view.
4.) and receive an email with the same link.
If I understand it well, the "tester's invitation id" acts as a token in order to track from which testers the invitation come from.
My questions:
1.) How can I generate the id in the link? I cannot use before_create cause the invitation id is not already set up when a tester registered.
I tried this:
in the invitation controller
def coming_soon
#invitation = Invitation.last
end
in views/invitations/coming_soon.html.erb
...
Copy and paste the following link to share wherever you want!</p>
<%= text_field_tag 'code', root_url + "?reffered_by=" + #invitation.id.to_s %>
Do you think they do like this?
2.) Why there is a question mark in the link? (or something like ?reffered_by= why not just root_url/#invitation.id.to_s) This is something related to the routes? is it a get method?
Thanks for your help!
I will answer question 2:
? in url is a way to pass parameters. A form using GET method uses this sort of appending to url to pass parameters. URL ?reffered_by=somevalue passes the parameter referred_by with value somevalue. Now if in our controller, we want this value, we can call params[:referred_by] and we will get the value as "somevalue". If we want to pass more than one parameter, we can pass it using &. For example,
"#{root_url}?referred_by=#{#invitation.id}&referred_time_stamp=#{Time.now.strftime(%Y%m%d%H%M%S)}"
Now, we can access these parameters in controller as params[:referred_by] and params[:referred_time_stamp].
If you have no routes or resources defined in routes.rb, you cannot pass parameters like id using
"#{root_url}/#{#invitation.id}". For example, when you define
map.connect ':controller/:action/:id'
in routes.rb, then it understands the part coming after last slash is the value of parameter id and we will get it as params[:id] in the controller.