1

我是一个菜鸟,在这个问题上苦苦挣扎的时间比我愿意承认的要长。我的 STI 模型曾经有一个路由问题,但现在我认为这已经解决了(感谢 SO)。

我的问题是,一旦我在以下位置更新表格:/kids/1/edit,而不是保存记录,它似乎再次获得记录。我知道我遗漏了一些基本的东西,但是在解决这个问题很长时间之后,我却没有找到答案。您可以看到我在表单提交中明确调用了 Kidupdate 操作。

提前致谢。

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

路线.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]

这是服务器日志中的内容:

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"=>"drefford@example.com", "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)

路线

$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 回答 1

1

正如第一条评论所说,表单应该使用 update_path 方法:

<%= form_for(@kid, url: kidupdate_path) do |f| %>
于 2012-11-05T13:40:47.147 回答