Sebastialonso
Sebastialonso

Reputation: 1461

Not able to show user data in form, getting undefined method `user_path' (Using devise and simple_form)

I'm trying to show the data from the user through a form so he can be able to edit any field. I'm using simple_form for this, and devise for authentication purposes. I keep getting the undefined method user_path error.

This is my controller

class HomeController < ApplicationController
 before_filter :authenticate_user!
 def index
 #Mostramos menu de opciones
 end

 def perfil
   @user = User.find(current_user.id)
 end

 def editar
   @user = User.find(current_user.id)
 end

end

The view for perfil.html.erb

<h1>showing user data</h1>
<%= simple_form_for @user do |f| %>
  <%= f.input :nombre %>
  <%= f.input :username %>
  <%= f.input :rut %>
  <%= f.input :ciudad %>
  <%= f.input :direccion %>
  <%= f.input :groupId %>
  <%= f.button :submit %>
<% end %>

<%= link_to "Editar datos", :action => :editar %>
<br>
<%= link_to "Volver", :action => :index %>

And my routes file

devise_for :users
match "perfil" => "home#perfil"
match "editar" => "home#editar"

The error is around this line in the view <%= simple_form_for @user do |f| %> I've been searching for guides or insights on what I'm doing wrong (I'm almost sure is something bad done or missing in routes.rb). Any insight would be much appreciated.

Upvotes: 0

Views: 885

Answers (3)

Viktor Tr&#243;n
Viktor Tr&#243;n

Reputation: 8884

if @user is an existing user, then

<%= simple_form_for @user do |f| %>

posts the form to user_path(@user) with PUT/PATCH http request type. Note that you need to create this route though. It is not clear from your code where this update action is, but if you used a resourceful pattern, then:

resources :users

takes care of it (routed via UsersController#edit), including your perfil and editar actions. BTW they look like they do the same: bring up a page to submit profile changes, normally routed done via UsersController#edit.

Upvotes: 1

Jiř&#237; Posp&#237;šil
Jiř&#237; Posp&#237;šil

Reputation: 14402

<%= simple_form_for @user do |f| %>

This would post to the form to users_controller controller and action depending on the context. devise_for :users does not generate that controller for you, instead you have registrations_controller available and in fact, you don't need to create the edit form, there's already one available at url users/edit for the current user.

To customize the default templates, run rails generate devise:views and take a look at app/views/devise.

Upvotes: 1

Sri
Sri

Reputation: 2273

Change your routes as following and try,

    devise_for :users
    match "perfil" => "home#perfil", :as => :user
    match "editar" => "home#editar", :as => :user

The :as parameter tells the router what to name the route as (You can then add _path or _url to whatever the :as parameter is).

Also, any time you link directly to an ActiveRecord model (e.g. link_to user.email, user), it will try to turn user into user_path.

Upvotes: 1

Related Questions