Nomethoderror in Users#show

Asked

Viewed 128 times

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) %>

In fact the book only goes up to the code edit_user_path, but I wanted to do some tests and I don’t understand why when I use thehow_user_path it says that the method does not exist instead of returning the same user, when trading for only

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

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

  • What is the exact error message? (including the file and the line)

  • But you want to show the profile from which view? You didn’t use scaffold?

3 answers

1


There is a command that assists in visualizing your routes. When you access a URL like /users/1/edit, using edit_user_path, it redirects you to the action edit of your users_controller.
This command is the rake routes. Running it on the command line in the base folder of your project you will see that the route that redirects to the action show 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.

0

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

Example:

rails generate scaffold user nome:string idade:integer

(Rails g scaffold name_do_model attribute:type)

  • I know from scaffold, this is for learning, I wonder why an edit_user_path works, but a show_user_path returns an error, by deduction I thought the action would be name_user_path

  • But you are using that path within which view?

  • Inside the show.html.erb in apps/users/views

  • But do you want show inside show? Kind of illogical... But you do show in what? Which user? I think you’re making a mistake because you’re already inside Show

  • Yeah, it’s not for logic, it’s for learning anyway

0

To see all the routes you have, you can go to browse and put the following address: http://localhost:3000/Rails/info/Routes

You will see that you will have some different routes, examples:

index:

users_path

new:

new_user_path

Edit:

edit_user_path
  • it is necessary to pass the parameter (id)

and the others (show, update and Destroy):

user_path
  • you need to pass the parameter (id) and also the method (GET to show, PATCH/PUT to update, DELETE to Destroy)

To the SHOW would look that way:

<%= link_to 'Mostrar Perfil', user_path(@user) %>

To the DESTROY would look that way:

<%= link_to 'Mostrar Perfil', user_path(@user), method: 'delete' %>

In the case of UPDATE you will do this in the controller, getting the action parameters EDIT. The :D method need not be specified

Browser other questions tagged

You are not signed in. Login or sign up in order to post.