1

我在单一资源 ( ) 中有一个嵌入式资源,并且在使用助手/profile/workout时遇到了一些问题。form_for

我已经定义了以下帮助程序,因为profile它只是基于current_user(只是重定向到正确的 url):

def workout_path(*args)
    profile_workout_path(*args)
end

我有以下模型:

class Workout
  include Mongoid::Document
  include Mongoid::Timestamps

  field :name, type: String

  embedded_in :user
  embeds_many :movements
  accepts_nested_attributes_for :movements
end

控制器:

def new
  @workout = current_user.workouts.build
  @workout.movements.build
end

路线:

ComposerDelete::Application.routes.draw do
  authenticated :user do
    root :to => 'home#index'
  end

  root :to => "home#index"
  devise_for :users
  resources :users do
  end

  resource :profile do
    resources :workouts
  end
end

和形式

= form_for @workout do |f|
  %fieldset
    = f.label :name
    = f.text_field :name
    = f.fields_for :movements do |builder|
      = render "movement_fields", f: builder
    = link_to_add_fields "Add Movement", f, :movements
    = f.submit "Create"

当我访问 url:http://localhost:3500/profile/workouts/50b99b70f0f800cd53000002/edit时,表单具有以下标题:

<form accept-charset="UTF-8" action="/profile/workouts/50b99b70f0f800cd53000002" class="edit_workout" id="edit_workout_50b99b70f0f800cd53000002" method="post">

它得到了正确的 id ( edit_<model>_<id>),但错误的方法 ( post, 应该是put),而且,提交按钮说Create而不是Update. 该表格可以正常工作,并更新锻炼。

耙路线:

                    root        /                                    home#index
                    root        /                                    home#index
        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
                   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
        profile_workouts GET    /profile/workouts(.:format)          workouts#index
                         POST   /profile/workouts(.:format)          workouts#create
     new_profile_workout GET    /profile/workouts/new(.:format)      workouts#new
    edit_profile_workout GET    /profile/workouts/:id/edit(.:format) workouts#edit
         profile_workout GET    /profile/workouts/:id(.:format)      workouts#show
                         PUT    /profile/workouts/:id(.:format)      workouts#update
                         DELETE /profile/workouts/:id(.:format)      workouts#destroy
                 profile POST   /profile(.:format)                   profiles#create
             new_profile GET    /profile/new(.:format)               profiles#new
            edit_profile GET    /profile/edit(.:format)              profiles#edit
                         GET    /profile(.:format)                   profiles#show
                         PUT    /profile(.:format)                   profiles#update
                         DELETE /profile(.:format)                   profiles#destroy
4

1 回答 1

1

该方法将始终是POST,而不是PUT- 您的浏览器本身不支持PUT,因此 Rails 通过在表单中​​使用隐藏的表单字段来解决这个问题_method=PUT。有关这方面的一些文档,请参见此处

另外,您自己的代码说= f.submit "Create",所以难怪按钮说“创建”。

于 2012-12-04T07:27:41.330 回答