我正在从事一个非常简单的项目,以更好地学习 Rails,来自 C# 背景。我的代码似乎呈现了不正确的表单操作。
它目前有 2 个模型正在使用
resources :leaks
resources :passwords
这是给我基本路线:
Matts-MacBook-Pro:pwcrack-webgui mandreko$ rvm 1.9.3 do rake routes
leaks GET /leaks(.:format) leaks#index
POST /leaks(.:format) leaks#create
new_leak GET /leaks/new(.:format) leaks#new
edit_leak GET /leaks/:id/edit(.:format) leaks#edit
leak GET /leaks/:id(.:format) leaks#show
PUT /leaks/:id(.:format) leaks#update
DELETE /leaks/:id(.:format) leaks#destroy
passwords GET /passwords(.:format) passwords#index
POST /passwords(.:format) passwords#create
new_password GET /passwords/new(.:format) passwords#new
edit_password GET /passwords/:id/edit(.:format) passwords#edit
password GET /passwords/:id(.:format) passwords#show
PUT /passwords/:id(.:format) passwords#update
DELETE /passwords/:id(.:format) passwords#destroy
在我的leakscontroller.rb 中,我有两种方法来尝试创建一个新的泄漏:
def new
@leak = Leak.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @leak }
end
end
def create
@leak = Leak.new(params[:leak])
if @leak.save
redirect_to @leak, :notice => "Successfully created leak."
else
render :action => 'new'
end
end
最后,在我的 new.html.erb 中,我有:
<%= form_for @leak, :url => { :action => "create" } do |f| %>
<div class="field>
<%= f.label :source %>
<%= f.text_field :source %>
</div>
<div class="field">
<%= f.label :file %>
<%= f.file_field :file %>
</div>
<div>
<%= f.submit %>
</div>
<% end %>
我认为使用此视图,它会创建一个带有 action=Create 的表单,但会生成以下代码:
<form accept-charset="UTF-8" action="/leaks" class="new_leak" enctype="multipart/form-data" id="new_leak" method="post">
任何线索为什么会这样?