Rails 3 don't find index page - ruby-on-rails

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

Related

Rails routing issue, can't figure out the link path

Let me fair from the outset, and tell you that I've 'solved' the problem I'm describing. But a solution that you don't understand is not really a solution, now is it?
I have a resource, Newsbites. I have an index page for Newsbites. All my CRUD actions work fine.
I created a separate index (frontindex.html.erb) that acts as the front page of my website to show the newest Newsbites. The formatting is different from my normal index so readers get a larger photo, more of the text of the article(more ads too:).
In my routing table, I have the following statements:
resources :newsbites
get 'newsbites/frontindex'
root 'newsbites#frontindex'
Rake routes show the following:
newsbites_frontindex GET /newsbites/frontindex(.:format) newsbites#frontindex
If I load my website from the root (localhost:3000), it works great. There is a separate menu page that is rendered at the top, and it loads fine. I can click on all links, except the 'Home' link, and they work fine.
The 'Home' link is:
<%= link_to 'Home', newsbites_frontindex_path %>
When I click on the linked, I get the following error:
Couldn't find Newsbite with 'id'=frontindex
The error points to the 'show' action of my Newbites controller. Here are the frontindex and show def from the controller. They appear exactly as I'm posting them:
def frontindex
#newsbites = Newsbite.all
end
def show
#newsbite = Newsbite.find(params[:id])
end
I don't get it. Why is the show action being called by newbites_frontindex_path when there is both a def and views that match? Now, I can get around this by simply pointing home to root_path. But that doesn't help me understand. What if this was not the root of the site?
Any help would be greatly appreciated.
Actually I'm very surprised your code worked at all. A route must define two things
Some sort of regex against which the URL of the user is matched (newsbites/frontindex is different than newsbites/backindex)
What do you want to do for a given URL ? You want to point to a controller action
Rails won't usually "guess" what that action is. Or maybe, he was still able to "guess" that you wanted to use the newsbites controller, but it didn't guess the action right this time :(.
You should declare the root like this, which is what you did
root 'controller#action'
For the rest, there are two ways you can declare it. I prefer the second one
resources :newsbites
get 'newsbites/frontindex', to: 'newsbites#frontindex'
resources :newsbites do
# stuff added here will have the context of the `newsbites` controller already
get 'frontindex', on: :collection # the name of the action is inferred to be `frontindex`
end
The on: :collection, means that 'frontindex' is an action that concerns ALL the newsbites, so the URL generated will be newsbites/frontindex.
On the other hand get 'custom_action', on: :member, means that the custom_action targets a specific item, the URL generated would look like newsbites/:id/custom_action
EDIT : Rails also generate path_helpers based on the route declaration
get 'test', to: 'newsbites#frontindex'
get 'test/something', to: 'newsbites#frontindex'
resources :newsbites do
get 'frontindex', on: :collection
get 'custom_action', on: :member
Will generate path helpers
test_path
test_something_path
# CRUD helpers : new/edit/show/delete, etc. helpers
frontindex_newsbites_path
custom_actions_newsbite_path(ID) # without s !
You can always override this by adding an as: option
get 'custom_action', on: :member, as: 'something_cool'
# => something_cool_newsbites_path
Rails routes thinks that frontindex is an id. That's what the error message says. So it goes to GET newsbite/:id which maps to show.
You need to find a way let Rails routes know that frontindex is not an id.
On a side note: The order in which you define routes matters. The first one matched will be used. If you have GET newsbite/:id and GET newsbite/frontindex then the one that appears first will be matched. In your case this is the first one.
Maybe try to change the order.

Accessing an arbitrary action in rails

I am somewhat confused as to how to properly access an action in a rails controller that is NOT associated with one particular model. The routing file, by default, seems to be routing the action name to "id". So if I type, say, /user/login, I end up with an error that says: "Couldn't find User with 'id'= login"
What is the proper way to access arbitrary action names in rails?
Make a route for it, obviously. The request goes this way:
first it hits a route
from there it hits a controllers' action
[an action may invoke a model] (optional, but common)
controller specified a view and fetches data to render
view is sent back in response to a request
resource and resources might not be obvious about what they do. But in fact, they are shorthands to corresponding collections of routes used quite often, like this is what resources adds. And they're not monoliths, they can be customised to fit your needs. Providing options is just a start, you can provide a block to define your custom action routes for this resource like so:
resources :users do
get :login
end
This will add a /users/login route that maps to UsersController#login, following Rails' conventions.
See this guide for more details and don't forget to run rake routes to see what you have at the moment.

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

RoR Routes: Why does the app assume I want the show action with every URL?

So, I'm trying to access other actions in my controller, and it assums I want "show" every time, when I really just want... the action I just had in my URL.
this is in my routes:
map.resources :attachments
And when I do
domain/attachments/any_action?params
I get this error:
ActionController::UnknownAction (No action responded to show. Actions: {list of actions}
Using rails 2.3.8
I think I know what you are getting at.
Yes show is assumed for singular uses and index for plural.
This is a convention.
Much of the power / magic in rails comes from "convention over configuration" and this is one of them. Yes it could have been /attachment/show/ or /attachments/index but why not just eliminate the show and index if these are the most common and have them available as defaults and that is what rails does.
Now, as for actions not being available, lets look at that. First of all, right off the bat, please do a rake routes at the command line and see what you get. Also please indicate which actions you are trying. The restful setup "resources" will make 7 actions available:- index, show, new, create, edit, update and destroy, but not "any action"
The rule in Rails is first route matched, first route served.
Because you define a resource, it will consider everything like domain/attachments/whatever to be show action for the whatever stuff.
So two choices here:
define your other routes and declare them before the resource
nest your other routes inside the resource (I don't know how to do that in Rails 2.x)

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.

Resources