Rails不使用顯式操作

[英]Rails form not using explicit action


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).

我是一個長期以來一直在努力解決這個問題的菜鳥。我曾經對我的STI模型有路由問題,但現在我認為已經解決了(感謝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.

我的問題是,一旦我更新表格:/ kids / 1 / edit,而不是保存記錄,它似乎再次得到記錄。我知道我遺漏了一些基本的東西,但在解決了這個問題很長一段時間之后我的答案就沒有了。您可以看到我明確地使用表單提交調用了kidupdate操作。

thanks in advance.

提前致謝。

kidedit.html.erb

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

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

的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"=>"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)

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

1 个解决方案

#1


1  

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

正如第一條評論所說,表單應該使用update_path方法:

<%= form_for(@kid, url: kidupdate_path) do |f| %>

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2012/11/05/720b1f94528096d3135efe0789d9b036.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com