Stack Overflow-like URL routes in rails 3 - ruby-on-rails

So I want my app to generate routes like stack overflow
questions/:id/:title
How can I do this in rails?

Please note that:
http://stackoverflow.com/questions/4434266/stack-overflow-like-url-routes-in-rails-3
http://stackoverflow.com/questions/4434266/
http://stackoverflow.com/questions/4434266/you-can-put-wathever-you-want-here
Are the same. I guess stackoverflow just does that in order to provide some context if you see just the link there.
So, your route would be just this one:
http://stackoverflow.com/questions/4434266/
which should be something like:
http://stackoverflow.com/questions/:id
You can ignore the rest of the url

This is what I was looking for:
http://norman.github.com/friendly_id/file.Guide.html
hope this can help others!

Everything you could ever want to know about routes: http://guides.rubyonrails.org/routing.html
Take a look specifically at http://guides.rubyonrails.org/routing.html#static-segments. What you want to do is create a route that maps to the controller and passes the question id and title as paramaters. Then your controller action will grab that information and render the correct view. The title attribute has to be accessible in your controller, it can't be only accessible in your view.

Related

Rails get Model :id via Post

I am looking for a way to access a model via its :id.
My project looks like this:
First someone can register himself in a form. Then he gets forwarded to a page where he can edit the things he entered. Now I do not want that you can see something in the URL.
Edit:
I was maybe a little unclear:
There is a form, where you can enter some things. After you submitted those things, you will be forwarded to another page with an URL like 'www.example.com/entry'. There I want to show what the person entered. And I do not want an URL like 'www.example.com/entry?id=12'
I hope that clarified some things
Your answer is a bit lacking in details, so I'll do my best here. Essentially, if you do not want to display the url parameters, then you will have to use the post HTTP method to submit your forms (which you should be doing anyway).
In your routes.rb file, you'll need to define your route to look something like this:
post 'route', to: 'controller#action'
Data submitted via the post method is typically submitted via a form. I would recommend using rails conventions like:
the rails form_for helper --> more details
resources since they typically give you the routes you want. To modify your routes beyond the defaults, I'd advise looking at the rails routing guide.

How to add a view in rails

I am quite new to rails but i have searched a lot how to do this but it doesnt seem to work for me. Im trying to create a new view called request for my model called steppy_steps, so i created a new file in the views directory called request.html.rb, added this to my routes, match '/request' => 'pages#request', also tried get "steppy_tests/home", and lastly added (def request, end), to my Steppy_Tests_Controller.rb but when i check the url it gives me an error:Couldn't find SteppyTest with id=home
I cant figure out what to do any help would be great! Thanks in advance.
You should read up on the MVC programming pattern (which is what Rails is based on)
In order to make a view, you need to have the controller and model aspects in place too. I think you're doing this already, but to help you understand more, I'll outline below:
Views : Controller Actions
If you want to show a view from the steppy_steps controller, you need to first have a controller action set up to handle the request. You'd normally use a self-named controller for this (controller name steppy_steps), and have various actions for that
In your routes, you'll then "capture" the request to your steppy_steps controller like this:
#config/routes.rb
resources :steppy_steps
This will create a set of RESTful routes, which you can then translate into your controller, like this:
#app/controllers/steppy_steps_controller.rb
def index
#Index code
end
def show
#Show code
end
This will allow you to create a views directory which can contain the views for /views/steppy_steps/show.html.erb and /views/steppy_steps/index.html.erb
Routes Are Super Important
The error you're getting is caused by you sending /home to your view
The problem here is that if you're using a route which has an in-built id param (the show action routes have this), then Rails will look for the extra params after the URL
To fix this, you'll have to provide more of your code, but I also believe you'd be better understanding the fundamentals of Rails a little more
Adding Routes
You can add routes & views as you wish
If you're looking to add a requests route / view, I'd do this:
#config/routes.rb
resources :steppy_steps do
collection do
get :requests
end
end
This will allow you to create /steppy_steps/requests

Rails 3.1 Routing

I'm trying to route a particular object's show method to the root path of my app, but I'm having trouble routing it anywhere at all for that matter. So I guess this break down into two questions:
If I wanted /pages/2 for example to go to the root path, how would I do this?
Also, if I wanted to make the url take a name or some attribute (eg. /username would find_by_username and show the correct page) how would I do this?
I've looked at a couple railscasts and the rails guide on routing, but I seem to be missing something...
Any help would be much appreciated.
First of all, I should warn you. Because you want to break down REST pattern. REST was well designed by clever folks and if you want to redefine some of it's functionality - so you have some incorrect logic in your App design.
Now to answer your question:
1. You can try something like this:
match "pages/:id", redirect {|params| "your_root_url" if params[:id] == 2}
2. Look at to_param documentation for more detail.
I hope i helped you alittle

Creating a rails path for a known action based on current record

As part of my template I show some automatic links which are created in the application_controller. Some of these links will offer a navigation direct from a show page to a corresponding edit page, and a few similar things.
My current code includes:
nav_links << {:name=>:edit,:url=>url_for(:action => :edit)}
This works great apart from I have child resources defined in my routes.rb file.
In this case instead of navigating to:
a/1/b/2/edit
It navigates to
b/2/edit
which isn't a valid route.
Any ideas on how to accomplish this would be much appreciated. I'm keen to avoid special case where at all possible.
A little confused - why not just use the routes resources define for you. Given that you mentioned 'child resource', I can assume you've declared these resources? If so - you should have a host of routes available to you...
Fire off 'rake routes' from the command line
Find your route
It will most likely take two arguments and look something like this (roughly):
edit_a_b GET /a/:a_id/b/:id
Just use edit_a_b_url(#a, #b)
Hope this helps.

Rails 3 don't find index page

I'm having a little trouble here with Rails 3.
I've created a controller called Admin. I've also defined a method called index and a view in app/views/admin/index.html.erb.
The problem is that when i go to http://localhost/myapp/admin i get a "Route not Found Error", but when I go to http://localhost/myapp/admin/index i get to the page I want.
I'm posting some links to the code, so you can check if there is any error, ok?
http://pastebin.com/UctUq44G
Thanks!
The issue is your routes.
There is no route for 'admin/'. This will add additional routes needed.
resources :admin
If your intention is to simply have semi static pages you can add
get 'admin'
Also some handy information :
http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

Resources