Rails 4 only routing to Index - ruby-on-rails

Ok so i'm having an issue. I'm creating a blog like application into my existing application. I setup up in my routes.rb a route for my posts like below:
#config/routes.rb
get 'posts/:id' => 'posts#index'
resources :posts
So i have all my files in my views such as:
index.html.erb
edit.html.erb
show.html.erb
new.html.erb
_form.html
So now when I got localhost:3000/posts/new I get a blank page where my edit.html.erb should have loaded my _form.html. Watching the Rails Server Console the server is not rendering 'posts/new.html.erb' it's rendering 'posts/index.html.erb'. It renders 'post/index.html.erb' regardless of what page I try to visit. Any advice here would be appreciated.

In your routes you put...
get 'posts/:id' => 'posts#index'
first. This means that pretty much all other GET routes, like posts/new will match against the posts/:id (the id will be "new")
I would just remove that line. The resources :posts will map posts/:id to posts#show which is where it really belongs.

Related

i have 3 nested routes: class/post/comments and i don' t have idea how to code the form_for in the _form file

If a have 2 nested routes: post/comments, the form_for is like this (in the _form file):
form_for([#post, #post.comments.build])
But in this case I have 3 nested routes: class/post/comments, and I don't know how to code it in the _form file.
Or there is other alternative to nest 3 routes?
config/routes.rb
Rails.application.routes.draw do
resources :campus do
resources :salas
end
devise_for :users
resources :cursos do
resources :publicacions do
resources :comentarios
end
end
get 'welcome/index'
root 'welcome#index'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
curso_publicacion_comentarios_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios(.:format)
comentarios#index
POST /cursos/:curso_id/publicacions/:publicacion_id/comentarios(.:format)
comentarios#create
new_curso_publicacion_comentario_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios/new(.:format)
comentarios#new
edit_curso_publicacion_comentario_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id/edit(.:format)
comentarios#edit
curso_publicacion_comentario_path GET /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#show
PATCH /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#update
PUT /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#update
DELETE /cursos/:curso_id/publicacions/:publicacion_id/comentarios/:id(.:format)
comentarios#destroy
Just follow the same pattern:
form_for([#class, #post, #comment])
But be advised that you have some problems with you code:
Don't use class to name variables, association and so, because it is used by Ruby and it will drive you into trouble.
Do not nest routes too much. As Rails guide says, nesting more than 1 level should be avoided.
Do not initialize objects in the form definition. Do it in the controller action or you won't be able to display validation error.

How to set route for a rendered action? Rails

i'm new to mvc, rails and web development and i'm facing a problem:
I have an action(show) and a view for this action.
The view for show submits a form_tag to another action, that renders the action show.
The problem is, I have no idea how to set a route for the action that renders show.
Right now my routes.rb is:
resources :meals do
collection do
get "meals/:id", to: "meals#show"
end
end
Tried to add these but didn't work:
match "meals/:id/calculate" , :to => "meals#calculate",:via => [:get]
and:
get "meals/:id/calculate", to => "meals#calculate"
resources :meals generate path to show action.
Run rake routes or open in a browser 'http://localhost:3000/rails/info/routes` to see list of generated routes.
To add calculate use member:
resources :meals do
get :calculate, on: :member
end

Couldn't find Wiki with 'id'=

When I try to load the show view, displaying a "wiki" that was created, I get the error message Couldn't find Wiki with 'id'=
It specifically points to the second line in the Controller:
def show
#wiki = Wiki.find(params[:id]) ## shows the error in this line
end
def index
#wikis = Wiki.all
end
The following is my view:
<h1><%= #wiki.title %></h1>
<h3><%= #wiki.body %></h3>
And the following are my routes:
Rails.application.routes.draw do
get 'wikis/index'
get 'wikis/show'
get 'wikis/new'
get 'wikis/edit'
get 'wikis/create'
devise_for :users
get 'welcome/index'
root 'welcome#index'
end
Why am I getting this error message, and how can I get the view to load?
Thank you.
There is no :id param unless you pass it in explicity according to how your routes are set up. That is, you'd have to go to /wikis?id=<some id>, which is probably not what you want. Consider a more standard resourceful route setup:
Rails.application.routes.draw do
resources :wikis
devise_for :users
get 'welcome/index'
root 'welcome#index'
end
which lets you go to /wikis/<id> (that is, wiki_path(#wiki) in the controller) for the show route, and defines the index, create, etc routes for you correctly. http://guides.rubyonrails.org/routing.html
You have this error because you don't pass wiki's id into your show link, so the simplest solution is providing an id:
<%= link_to wiki.title, wikis_show_path(id: wiki) %>
But to be honest, your code looks very ugly with these custom routes to wikis controller which can be replaced by putting simply:
resources :wikis
in your routes.rb. This way, link to single wiki show page is also simpler:
<%= link_to wiki.title, wiki %>
You may want to read Rails routing guide.
Your params[:id] is most likely nil unless you pass it like /wikis/show?id=123, because route get 'wikis/show' does not have any params.
Most common approach is to have resources :wikis and routes like /wikis/123
add this line in your routes
resources :wikis
and remove this code:
get 'wikis/index'
get 'wikis/show'
get 'wikis/new'
get 'wikis/edit'
get 'wikis/create'
Now you can write in url: websitename.com/wikis to index, websitename.com/wikis/id, websitename.com/wikis/id/edit to edit... and get your id param...
Read more about resources in this link: Resources Link

Simple Navigation Routing Error in Rails 3.2.8

longtime reader & first time poster, so I'd appreciate it if you went easy on me.
Recently started teaching myself RoR, and have been hacking away at a personal project/website to get the hang of things. Here's my problem:
I'm using the Simple Navigation gem to generate links. Inside navigation.rb I'm trying to call:
primary.item :home, 'Home', home_path
...where home is a view and controller that displays my front page:
home > index.html.erb (just contains a bunch of standard HTML, but let me know if it'd be useful to include)
and controllers > home_controller.rb:
class HomeController < ApplicationController
def index
#posts = Post.all
end
end
I'm getting this error, though:
Routing Error
No route matches {:action=>"show", :controller=>"home"}
Try running rake routes for more information on available routes.
... so I run rake routes, and can definitely see "home#show" in there.
My routes.rb file, as well, has this in it:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index"
resources :posts
resources :home
So I'm a little baffled, and I'm sure it's because of my inexperience or general inability to understand what I'm doing, but I'd really appreciate some help as it's more or less a road block that I haven't been able to overcome.
Appreciate it!
Jay
It is because of the resources; if you are not using the resources remove resources :home
This could be your routes:
get "projects/index"
get "offer/index"
get "space/index"
get "home/index", :as => "home"
resources :posts
See how I removed the resources :home. In home/index the :as represents an alias, so you can use the alias as a method, adding "path" at the end of the name.
Check this guide about routes and resources: http://guides.rubyonrails.org/routing.html#resource-routing-the-rails-default

My routes has resources :home, but rspec is saying routes not defined?

My homecontroller has:
def about()
end
And I have a rspec test that does GET 'about' and it fails saying that there is no route that matches.
doesn't this map all actions in the controller:
resources :home
or do I have to explicitly state each action in the home controller?
resources :home sets up the default RESTful routes - index, show, new, create, edit, update, and destroy. Any additional routes have to be specified. It looks like you're adding a simple collection route, so you'd specify it like this:
resources :home
collection do
get 'about'
end
end
This will give your the route '/home/about'. I assume this is Rails 3. If you're in Rails 2.x, do it like so:
map.resources :home, :collection => {:about => :get}
And from the command line, you can always see what routes you have available with this command:
rake routes
I hope this helps!
EDIT: If you want a default route, you can add this:
match ':controller(/:action(/:id))'
This is a default route that will match any generic requests.
FULL ARTICLE: Routing in Rails 3 is its own beast. There have been a lot of questions about it lately, so I've created a very detailed article with code samples to help others:
Routing in Ruby on Rails 3
I created a companion Rails 3 app that can be downloaded to play around with, as well:
https://github.com/kconrails/rails3_routing
If you have any questions, please hit up my site and ask. Thanks!
resources will give you the 7 CRUD methods for a controller, if you want additional actions, you need to do something like the following:
resources :homes do
collection do
match "about" => "homes#about", :as => "about"
end
end
Then you'll also have an additional about_homes_path/url helper available.

Resources