1

I'm a noob that's been struggling with this problem for longer than I care to admit. I used to have a routing issue with my STI model, but now I think that's solved (thanks to SO).

My problem is once I update the form at : /kids/1/edit, instead of having the record saved, it seems to get the record again. I know I'm missing something basic, yet after working the issue a long time the answer eludes me. You can see I'm explicitly calling the kidupdate action with the form submission.

thanks in advance.

kidedit.html.erb

<% provide(:title, "Edit user") %> 
<h1>Update your profile</h1>

<div class="row">
  <div class="span5 offset3"> 
    <%= form_for(@kid, url: kidedit_path) do |f| %>
      <#%= render 'shared/error_messages', object: f.object %> 

      <%= f.label :fname, "First Name" %>
      <%= f.text_field :fname %> 

      <%= f.label :lname, "Last Name" %>
      <%= f.text_field :lname %> 

      <%= f.label :email %>
      <%= f.text_field :email %> 

      <%= f.label :type, "Are you a Kid or Parent" %>
      <%= f.select :type, [['Kid','Kid'],['Parent','Parent']] %>

      <%= f.label :password %>
      <%= f.password_field :password %>

      <%= f.label :password_confirmation, "Confirmation" %>
      <%= f.password_field :password_confirmation %>

      <%= f.submit "Save changes", class: "btn btn-large btn-primary", :controller => 'users', :action => 'kidupdate' %>
  <% end %>
  </div>
</div>

users_controller.rb

  def kidupdate

    @kid = Kid.find(params[:id])
    if @kid.update_attributes(params[:kid])
      flash[:success] = "Profile updated"
      sign_in @kid
      redirect_to kidshow_path
    else
      render kidedit_path(@kid)
    end
  end

routes.rb

Kidtunes::Application.routes.draw do

  root to: 'static_pages#home'

  match '/help',    to: 'static_pages#help'
  match '/contact', to: 'static_pages#contact'
  match '/signup',  to: 'users#new'
  match '/signin',  to: 'sessions#new'
  match '/signout',  to: 'sessions#destroy', via: :delete

  match 'kids/:id' => 'users#kidupdate', via: :put, :as => :kidupdate
  match 'kids/:id' => 'users#kidshow', via: :get, :as => :kidshow
  match 'kids/:id/edit' => 'users#kidedit', :as => :kidedit

  resources :users
  resources :sessions, only: [:new, :create, :destroy]

Here's what's in the server log:

Started PUT "/kids/1/edit" for 127.0.0.1 at 2012-11-05 07:52:28 -0500
Processing by UsersController#kidedit as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"T8RqFt9lxdbZU+1cOh2E5yu2CFbVRDGmRcj2XdDN1ZU=", "user"=>{"fname"=>"Dante", "lname"=>"Refford", "email"=>"[email protected]", "type"=>"Kid", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Save changes", "id"=>"1"}
  Kid Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."type" IN ('Kid') AND "users"."id" = ? LIMIT 1  [["id", "1"]]
  Rendered users/kidedit.html.erb within layouts/application (4.1ms)
  Rendered layouts/_shim.html.erb (0.0ms)
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."remember_token" = '1RZr3qfB6QSh42_jQ9qNWQ' LIMIT 1
  Rendered layouts/_header.html.erb (2.5ms)
  Rendered layouts/_footer.html.erb (0.3ms)
Completed 200 OK in 67ms (Views: 46.3ms | ActiveRecord: 1.0ms)

Routes

$rake routes
       root        /                         static_pages#home
       help        /help(.:format)           static_pages#help
    contact        /contact(.:format)        static_pages#contact
     signup        /signup(.:format)         users#new
     signin        /signin(.:format)         sessions#new
    signout DELETE /signout(.:format)        sessions#destroy
  kidupdate PUT    /kids/:id(.:format)       users#kidupdate
    kidshow GET    /kids/:id(.:format)       users#kidshow
    kidedit        /kids/:id/edit(.:format)  users#kidedit
      users GET    /users(.:format)          users#index
            POST   /users(.:format)          users#create
   new_user GET    /users/new(.:format)      users#new
  edit_user GET    /users/:id/edit(.:format) users#edit
       user GET    /users/:id(.:format)      users#show
            PUT    /users/:id(.:format)      users#update
            DELETE /users/:id(.:format)      users#destroy
   sessions POST   /sessions(.:format)       sessions#create
new_session GET    /sessions/new(.:format)   sessions#new
    session DELETE /sessions/:id(.:format)   sessions#destroy
4
  • 1
    Can you confirm that the URL path to where the form points to is kidupdate_path and not kidedit_path? From what I can tell, the form is pointing to the latter which renders the form again. Also, I see you're overriding the form action in the submit button, was that intended? Commented Nov 5, 2012 at 13:00
  • josemota is right, you point the wrong action in the form, and the action setting in the submit button is useless, remove it. Also I would recommend you to use different path for update and show as it can mix up things. Commented Nov 5, 2012 at 13:12
  • Are you redirecting to show page or edit page after submit? Commented Nov 5, 2012 at 13:14
  • Regarding overriding the action. This is intentional. I have two User subclasses: Kid & Parent. There's a lot of overlap between these two subclasses except for show, edit and update. I have all the actions in the Users controller and therefore I need a way to differentiate between users#kidedit and users#parentedit Commented Nov 5, 2012 at 13:29

1 Answer 1

1

As the first comment said, the form should be using the update_path method:

<%= form_for(@kid, url: kidupdate_path) do |f| %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.