Topic: my Forms is not saving

I think that my problem is with Device and that i have 2 Controllers for Users.
but i don't really know how to troubleshoot this to be true.

I don't know where the controller for Device is

What i know is when i hit save on my form it take me back to the root_path


controller

def create
    @user = User.new(params[:user])
    if @user.save
      flash[:success] = "User Created"
      redirect_to @user
    else
      render 'new'
    end
  end

Re: my Forms is not saving

It doesn't matter how many controllers you have for a given Model. That shouldn't be the problem. The key is which controller is your form sending to and is that actually what's processing.

Are you talking about the devise plugin

Last edited by matthewvb (2012-05-31 14:44:01)

matthewvb

Re: my Forms is not saving

Thanks for the help.
I read that article and i saw the rails cast and its really useful but i am still having problems with this.

i have the same problem where i submit the form it takes me to the hope page and does not add my record.
i was wondering if you know the code i can put someplace that would put me in debugger mode in the consul?

Re: my Forms is not saving

Not sure console will show you redirects.

Sounds like it's not directing to your create method but the devise method. If you made your own form, I'd verify the form_for information and the path you're submitting to.

matthewvb

Re: my Forms is not saving

my UserController

class UsersController < ApplicationController
  def index
    @user = User.all
  end
  def show
    @user = User.find(params[:id])
  end
  def new
    @user = User.new
  end
  def create
    @user = User.new(params[:account])
    if @account.save
      flash[:success] = "Account Created"
      redirect_to @user
    else
      render 'new'
    end
  end
  def edit
    @user = User.find(params[:id])
  end
  def update
    @user = User.find(params[:id])
    if @user.update_attributes(params[:user])
      flash[:success] = "User Updated"
      redirect_to @user
    else
      render 'edit'
    end
  end
  def destroy
    User.find(params[:id]).destroy
    flash[:success] = "User Destroyed."
    redirect_to user_path
  end
end

and its identical to another controller that does work


My User Model

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :first_name, :last_name, :account_id
  belongs_to :account
  validates_uniqueness_of :username, presence: true, :case_sensitive => false
  validates :first_name, presence: true
  validates :last_name, presence: true
  validates :password, presence: true
  validates :password_confirmation, presence: true
end

The Problem is that when i press the submit btn i get send to the hope page and there is no record created

Re: my Forms is not saving

app/views/users/new.html.erb

<%= form_for(@user) do |f| %>

HTML
<form accept-charset="UTF-8" action="/users" class="new_user" id="new_user" method="post">

Rake routes
users                                     GET             /users(.:format)               users#index
users_new                              GET    /users_new(.:format)           users#new
new_user_session                   GET    /users/sign_in(.:format)       devise/sessions#new
user_session                          POST   /users/sign_in(.:format)       devise/sessions#create
destroy_user_session             DELETE /users/sign_out(.:format)      devise/sessions#destroy
user_password                      POST   /users/password(.:format)      devise/passwords#create
new_user_password               GET    /users/password/new(.:format)  devise/passwords#new
edit_user_password               GET    /users/password/edit(.:format) devise/passwords#edit
                                            PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration         GET    /users/cancel(.:format)        devise/registrations#cancel
user_registration                    POST   /users(.:format)               devise/registrations#create
new_user_registration             GET    /users/sign_up(.:format)       devise/registrations#new
edit_user_registration              GET    /users/edit(.:format)          devise/registrations#edit
                                              PUT    /users(.:format)               devise/registrations#update
                                             DELETE /users(.:format)               devise/registrations#destroy

After doing this I see i have to send the form to go to /users_new instead of /users
And i have to use a POST instead of a GET

Last edited by moiseszaragoza (2012-05-31 14:58:46)

Re: my Forms is not saving

I'd recommend running the Devise generator:

rails generate devise:views

This will generate the views for the devise gem and let you customize them.

matthewvb

Re: my Forms is not saving

That i how i got started.
But i think that the problem is that is not meant to be used if i am already longed in


rails generate devise:views
      invoke  Devise::Generators::SharedViewsGenerator
       exist    app/views/devise/shared
    conflict    app/views/devise/shared/_links.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/shared/_links.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/shared/_links.erb
      invoke  form_for
       exist    app/views/devise/confirmations
    conflict    app/views/devise/confirmations/new.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/confirmations/new.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/confirmations/new.html.erb
       exist    app/views/devise/passwords
    conflict    app/views/devise/passwords/edit.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/passwords/edit.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/passwords/edit.html.erb
    conflict    app/views/devise/passwords/new.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/passwords/new.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/passwords/new.html.erb
       exist    app/views/devise/registrations
    conflict    app/views/devise/registrations/edit.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/registrations/edit.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/registrations/edit.html.erb
    conflict    app/views/devise/registrations/new.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/registrations/new.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/registrations/new.html.erb
       exist    app/views/devise/sessions
    conflict    app/views/devise/sessions/new.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/sessions/new.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/sessions/new.html.erb
       exist    app/views/devise/unlocks
    conflict    app/views/devise/unlocks/new.html.erb
  Overwrite /Users/moiseszaragoza/Sites/Sample/app/views/devise/unlocks/new.html.erb? (enter "h" for help) [Ynaqdh] n
        skip    app/views/devise/unlocks/new.html.erb
      invoke  erb
       exist    app/views/devise/mailer
   identical    app/views/devise/mailer/confirmation_instructions.html.erb
   identical    app/views/devise/mailer/reset_password_instructions.html.erb
   identical    app/views/devise/mailer/unlock_instructions.html.erb

Last edited by moiseszaragoza (2012-05-31 15:11:19)

Re: my Forms is not saving

Make sure your routes has:

devise_for :users

and you don't overwrite them with other :users routes

matthewvb

Re: my Forms is not saving

my routes look like this

devise_for :users do
    #resources :users
    get 'users' => 'users#index'
    get 'users_new' => 'users#new'
  end

probably wrong ...

Re: my Forms is not saving

The way I'm doing this is by  using the devise methods to create and edit. That way has the routes like:

devise_for :users
  resources :users do
    member do
      #extra actions here
    end
  end
matthewvb

Re: my Forms is not saving

i updated my routes file

devise_for :users
  resources :users do
    member do
    get 'users_new' => 'users#new'
    get 'users' => 'users#index'
      #extra actions here
    end
  end

now my rout for

<%= link_to "Add User", users_new_path  %> 

does not work
and if i update the link to

<%= link_to "Add User", new_user_session_path  %>

it takes me to the home page

Last edited by moiseszaragoza (2012-05-31 16:18:28)

Re: my Forms is not saving

Are you using the Devise new user form or your own?

matthewvb

Re: my Forms is not saving

i am using my own because when i go to
<%= link_to "Add User", new_user_session_path  %>
it takes me to the home page

i guess i have to figure how to actually stay on that page

Re: my Forms is not saving

It's a routing issue. Default devise route for new_user_session_path is for signing in, not registering. You should rake routes and see which brings you to your registration page.

should be like: new_user_registration_path

Last edited by matthewvb (2012-05-31 16:37:22)

matthewvb

Re: my Forms is not saving

That is what i have
<%= link_to "Add User", new_user_registration_path  %>

it takes me to <a href="/users/sign_up">Add User</a>


but if i am loged in and i go to that page it takes me to the home pate