Search code examples
ruby-on-railsroutessti

Rails STI: implementing child class edit form


There is a lot of discussion around Rails 3 STI and how to use forms, but no definitive answers on StackOverflow. I seem to have run into a similar issue and have attempted the other solutions with no results.

I have two models with the following inheritance set up:

User.rb
class User < ActiveRecord::Base
attr_accessible :email, :first_name, :last_name, #more follows


Waiter.rb
class Waiter < User

On the form at /waiters/users/[:id]/edit, I am attempting to create a form for editing the waiter. However, I am getting the following error upon loading the edit view:

undefined method `waiter_path' for #<#<Class:0x007fbd08cef9d8>:0x007fbd09532fa0>

This is my controller found at /app/controllers/admin/waiters/users_controller.rb:

  def edit
    form_info
    @user = Waiter.find(params[:id])
  end

  def update
    @user = Waiter.find_by_id(params[:id])
    if @user.update_attributes(params[:user])
      flash[:notice] = "Successfully assigned Waiter."
      redirect_to admin_waiters_users_url()
    else
      form_info
      render :action => 'edit'
    end
  end

And this is the form located in the edit view:

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

  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.input :email %>

      <%= button_tag(type: 'submit', class: "btn btn-primary") do %>
      <i class="icon-ok icon-white"></i> Save
      <% end %>
  <% end %>

What am I doing wrong here with STI and routing?

UPDATE: here is my rake routes:

admin_waiters_users GET    /admin/waiters/users(.:format)                                                            admin/waiters/users#index
                                                                POST   /admin/waiters/users(.:format)                                                            admin/waiters/users#create
                                        new_admin_waiters_user GET    /admin/waiters/users/new(.:format)                                                        admin/waiters/users#new
                                       edit_admin_waiters_user GET    /admin/waiters/users/:id/edit(.:format)                                                   admin/waiters/users#edit
                                            admin_waiters_user GET    /admin/waiters/users/:id(.:format)                                                        admin/waiters/users#show
                                                                PUT    /admin/waiters/users/:id(.:format)                                                        admin/waiters/users#update

Solution

  • You should use your routes to see what routes you have defined:

    You can run your routes with:

    rake routes
    

    I can not see your routes but perhaps waiter_path does not exist.

    Perhaps is user_waiter_path(@user) or other router.

    Please paste your routes for that the people on stackoverflow can help to you.

    I can not see the route waiter_path on your routes, If you have waiter_path inside of your edit view you have remove it.

    Also, you can specify what controller and action hit,

    <%= simple_form_for @user, :url => { :controller => "admin/waiters/users", :action => "update"} do |f| %>
     <%= f.input :first_name %>
     <%= f.input :last_name %>
     <%= f.input :email %>
     <%= f.button :submit, "save", class: "btn btn-primary"%>
    <% end %>
    

    You can check with f.button instead button_tag

    Regards!