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
Related
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.
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.
This is kind of difficult to communicate but I'll try without pasting all my code. I have Members who have one Mailbox which has many Receipts. In the header layout I have a nav that calls
<%= link_to "Message Center", member_mailbox_path(current_user.member_id) %>
It works on most pages like trails/# , the resource pages for various models
But on other pages, seems like custom route pages, I get this error
No route matches {:action=>"show", :controller=>"mailbox", :member_id=>16}
Running rake routes shows this:
member_mailbox GET /members/:member_id/mailbox/:id(.:format) mailbox#show
Routes are confusing to me, here are my routes for this problem (show message isn't tested yet) ...
resources :members do
resources :mailbox do
resources :receipts do
member do
get :show_message
end
end
end
end
The routes for the pages that are showing the error are similar to this one
match '/my_plays', :to => "trails#my_plays"
match '/my_creations', :to => "trails#my_creations"
So not sure if my routes are right. I wonder if resources :mailbox is correct since I don't have a bunch of resources for that, it's a has_one .... THX
----EDIT--- after changing route per advice:
member_mailbox POST /members/:member_id/mailbox(.:format) mailboxes#create
new_member_mailbox GET /members/:member_id/mailbox/new(.:format) mailboxes#new
edit_member_mailbox GET /members/:member_id/mailbox/edit(.:format) mailboxes#edit
GET /members/:member_id/mailbox(.:format) mailboxes#show
PUT /members/:member_id/mailbox(.:format) mailboxes#update
DELETE /members/:member_id/mailbox(.:format) mailboxes#destroy
You may want to define a mailbox as a singular resource in your routes. Otherwise, Rails will expect you to pass in both the user id and the mailbox id for member_mailbox_path to route to mailbox#show. I believe this is why you're getting a routing error. Since each user has one mailbox, there's no need to make this extra lookup part of the route. So instead of resources :mailbox, you can do resource :mailbox:
resources :members do
resource :mailbox do
resources :receipts do
member do
get :show_message
end
end
end
end
I believe this would generate the following routes:
member_mailbox POST /members/:member_id/mailbox(.:format) mailboxes#create
new_member_mailbox GET /members/:member_id/mailbox/new(.:format) mailboxes#new
edit_member_mailbox GET /members/:member_id/mailbox/edit(.:format) mailboxes#edit
GET /members/:member_id/mailbox(.:format) mailboxes#show
PUT /members/:member_id/mailbox(.:format) mailboxes#update
DELETE /members/:member_id/mailbox(.:format) mailboxes#destroy
Notice that the lack of path names next to GET, PUT, and DELETE doesn't mean they don't exist; they're just repeats of the POST path, but each responds to different HTTP methods.
To render mailboxes#show, you'll need to add a MailboxesController with a show route, which might do a look up for the member:
class MailboxesController < ApplicationController
def show
#member = Member.find(params[:member_id])
# other mailbox code...
end
end
And you'll also create a template at app/views/mailboxes/show.html.erb to render the mailbox show page.
Also, I would recommend against deeply nesting your routes, as in third level :receipts.
I am putting a randomize def in my controller and would like to access it with a restful route. The route should be accessed with the following:
<%= link_to "Randomize", random_reader_path %>
But I cannot figure out how to get this route to appear in rake routes or configure it correctly in my routes.rb file.
The random method will do the same thing as index only provide a random page content #variable
Currently I have my reader Controller as
resources :reader
in my routes.rb
Add more RESTful actions!
resources :reader do
get 'random', on: :collection
end
The route will be random_readers_path, though.
I want to pass a parameter to the index action, but the I'm only getting the show action.
routes.rb:
Test1::Application.routes.draw do
resources :blog
end
blog_controller.rb:
def show
# code
end
def index
# code
end
View url that send to show action instead to index action:
My link
What should I add in routes file or in view?
Output of my routes:
$ rake routes
blog GET /blog(.:format) {:action=>"index", :controller=>"blog"}
blog GET /blog/:id(.:format) {:action=>"show", :controller=>"blog"}
The command line will show you routes you can use with rake routes
The route you want is blogs_path and you can add a parameter on to that, e.g. blogs_path(other_item => :value).
Exactly how will depend on whether you are try to use it in a controller, another view, etc.
For the view have: <%= link_to 'My Link', blogs_path(:other_item => value) %>
It sounds like you want 2 routes:
/blogs/:other_param
/blogs/:id
But, for as smart as Rails is, it can't figure out whether the param is meant to be treated as an other_param or as an id.
So the simplest solution is to add this route to the resources defaults like so:
resources :blogs
get "/blogs/other_param/:other_param", to: "blogs#index", as: "other_param_blogs"
That way Rails knows that if you're going to /blogs/other_param/current, then it will treat current as the :other_param.
Use below code to pass parameter:
My link
or
<%= link_to "My link", blog_path(name: "test") %>
above code will redirect to index action with name as key and test as parameter,