link_to action and id

Asked

Viewed 235 times

0

I want to do an action to change the password and I have the following link_to to redirect to html.erb with the correct user

<%= link_to 'Mudar Senha', "edit_password_form/" + @usuario.id.to_s %>

I feel like I’m doing it the wrong way, the best way to do it?

  • Edit your question by adding more details. You’re using a Gem (like Devise, for example) for user authentication or created everything "on the same arm"? There is a usuarios_controller.rb created by you? How is your routes.rb hitherto?

1 answer

1

Check your project routes by giving the command rake routes from the console, this command will return a list with 4 columns.

  • The 4 column is the action of your controller that should be executed (e.g.: show).
  • 3 is the route as shown in the address bar .
  • A 2 is the sending method used by the action.
  • 1 is the shortcut to use redirecting.

Find the action you want to use in your controller in the 4 column and put it in place of the "edit_password_form/" + @user.id.to_s which appears in the first column + path if you need to specify an object pass it in the function (eg: edit_password_path(@usuario)).

If the first column is empty you should look at its method, it will be the same path as the top row, but you need to specify the method with the method attribute (eg: method: :put)

Otherwise, you should add the route to your file routes.rb (study about)

Browser other questions tagged

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