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'
Related
In My rails application, the users will enter a URL Which contains country name, like below.
example :
localhost:3000/usa
localhost:3000/uk
localhost:3000/canada
And my current logic in the Application is like this.
if ["usa","canada","uk"].include? (request.url.split("/").last)
# some logic
end
Is there any better way of writing the above condition?
I'd prefer define it in config/routes.rb
For example:
get "/countries/:name", to: "countries#show"
As a result you'll have params[:name] in the controller equal to country name from the route.
But if you have the exact list of countries, maybe you'll want to define a separate route for each of these.
get "/usa", to: "countries#usa"
get "/uk", to: "countries#uk"
get "/canada", to: "countries#canada"
I'm sorry, my English is bad. Question: I have model Pages with columns title, description etc. I can create, change, destroy these pages. I can see the contents of the link mydomain/pages/1. I need for each page has been template and route, so I can see the content on the link, for example maydomain/contacts. How to do it? Help me please.
One way to implement your own solution is to add this to your routes file:
get '/mydomain/:slug, to: 'pages#show'
This is a pretty general matcher, so add it to the bottom of your routes so it doesn't override others.
Then your controller show action will look something like:
def show
#page = Page.find_by_slug(params[:slug])
end
This of course assumes you have a slug column on your Pages table.
I'm assuming by "mydomain" you mean the root url of your site (e.g. myapp.example.com)
I'd suggest that you separate the problem into two parts:
Use an attribute other than id to identify an item in the url
Reduce the route so that the controller does not need to be specified.
For 1, have a look at this: Rails routes with :name instead of :id url parameters Note, that as #spickermann suggests friendly_id could be a good solution for you.
For 2, you will need to create a route without the controller name, and then specify the controller in the route definition. (See the Rails Routing Guide):
get ':param', to: :show, controller: 'pages'
For that to work, you will need to put it after (lower in routes.rb) so that it doesn't intefer with other routes. I'd also recommend adding a constraint to the route - to limit the wrong urls that could be routed to that rout.
I am trying to find a best-practice to allow users to define the route to their pages in Rails, by writing them in a text field when submitting posts, like with the Path module in Drupal (yes, we are porting a Drupal site to Rails)
So, I need to
define a new, named route on article submission (eg http://www.domain.com/a-day-in-annas-life)
change the existing route on article edit, if they define a new one, by doing a 301 redirect from the old route to the new one
How can I best achieve this?
Okay, I found a way, but if it's best practice or not, I cant say.
I am using custom restrictor's like this:
class CharitiesRestrictor
def self.matches?(request)
slug = request.path_parameters[:path]
!Charity.find_by_name(slug).nil?
end
end
constraints CharitiesRestrictor do
match '*path' => 'charities#show_by_slug', :constraints => CharitiesRestrictor.new
end
When I create a block like this for each of my model/controller pairs that should be able to respond to permalinks, I can have them all have a chance to act on a permalink. However, this also means that they all get called in series, which is not necessarily ideal.
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]
Say if I have a model called 'deliver' and I am using the default URL route of:
# Install the default routes as the lowest priority.
map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format'
So the deliver URL would be:
http://localhost:3000/deliver/123
What I am trying to work out, is how to use another field from the database alongside or instead of the ID.
For example. If I have a field in the create view called 'deliveraddress', how do I put that into the routes?
So I can have something link this:
http://localhost:3000/deliver/deliveraddress
Thanks,
Danny
Since it sounds from your comments like you're trying to obfuscate the ID in your URL, I would suggest that you look at this question, which was asked a few days ago.
Obfuscating ids in Rails app
First of all, the url "http://localhost:3000/deliver/123" matches either default routing rule. However, only after you declared an "resource" it will generate such a RESTful URL.
In your case, just implement the "to_param" method of Deliver model:
class Deliver < ActiveRecord::Base
def to_param
return self.deliveraddress
end
end
it will generate the url you want by calling url_for method, like link_to #deliver
Do not forget to make sure you have unique deliver addresses in your database so you will never find duplicated records with one address.
After that, you need to update the finder methods in actions:
def show
#deliver = Deliver.find_by_deliver_address!(params[:id])
end
Hope this answer will be useful.