NoMethodError in Users # show

2

I'm learning Rails, following a book that has the following code:

UserController

class UsersController < ApplicationController

 def new
   @user = User.new
 end

 def edit
    @user = User.find(params[:id])
 end

 def show
     @user = User.find(params[:id])
 end

 def create
   @user = User.new(params[:user])
   if @user.save
      redirect_to @user, :notice => 'Cadastro realizado'

   else
     render :new
   end
  end      
end

show.html.erb

<p id="notice"><%=notice%></p>

<h2>Perfil: <%[email protected]_name %></h2>

    <ul>
        <li>Localização: <%= @user.location %> </li>
        <li>Bio: <%= @user.bio %></li>
   </ul>

   <%= link_to 'Editar Perfil', edit_user_path(@user) %>
   <%= link_to 'Mostrar Perfil', show_user_path(@user) %>

Actually the book only goes up to the code edit_user_path , however I wanted to do some tests and I'm not understanding why when I use the how_user_path it says that the method does not exist instead of returning the same user, for only

<%= link_to 'Mostrar Perfil', @user %>

The code works, but I would like to know why with show_user_path it returns an error being that the method obviously exists in controller , my goal would be to show the profile that was created.     

asked by anonymous 05.02.2014 / 17:55

2 answers

1

There is a command that helps you to view your routes . When you access a URL as /users/1/edit , using edit_user_path , it redirects you to the edit action of your users_controller . This command is rake routes . Running it on the command line in the home directory of your project you will see that the route that redirects to the show action is different from show_user_path .

new_user GET users#new
edit_user GET users#edit
user GET users#show

As you can see, using user_path(@user) will use the correct route.

    
05.02.2014 / 18:43
0

As you are now learning, it is good to start by writing the code in the "nail" and understanding. But it has a command called scaffold, it generates a basic CRUD from just a command line.

Example:

rails generate scaffold user nome:string idade:integer

(rails g scaffold model_name attribute: type)

    
05.02.2014 / 18:11