我在一个单一资源(/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"
当我访问网址: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
{1}}。表单正常工作,并更新锻炼。
rake routes:
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
答案 0 :(得分:1)
该方法始终为POST
,而不是PUT
- 您的浏览器本身不支持PUT
,因此Rails通过使用{{{{1}中的隐藏表单字段来解决此问题1}}。有关此问题的一些文档,请参阅here。
另外,您自己的代码显示为_method=PUT
,因此按钮显示为“创建”也就不足为奇了。