Ruby on rails 3 Rails 3路线在表单编辑时发布
基本上,我可以创建一个新项,保存到数据库中的表中。但是,当我去编辑项目时,表单会打开,我进行更改,然后当我去提交时,它会将我带到与编辑页面相同的url,并给我发送错误No route matches“/support/14/edit”虽然如果在地址栏中输入,它会打开编辑表单,但不会保存任何更改。这是我的代码 routes.rbRuby on rails 3 Rails 3路线在表单编辑时发布,ruby-on-rails-3,forms,routing,Ruby On Rails 3,Forms,Routing,基本上,我可以创建一个新项,保存到数据库中的表中。但是,当我去编辑项目时,表单会打开,我进行更改,然后当我去提交时,它会将我带到与编辑页面相同的url,并给我发送错误No route matches“/support/14/edit”虽然如果在地址栏中输入,它会打开编辑表单,但不会保存任何更改。这是我的代码 routes.rb resources :support support_controller.rb def new @support_item = Support.new re
resources :support
support_controller.rb
def new
@support_item = Support.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @support_item }
end
end
# GET /support/1/edit
def edit
@support_item = Support.find(params[:id])
end
# POST /support
# POST /support.xml
def create
@support_item = Support.new(params[:support_item])
respond_to do |format|
if @support_item.save
format.html { redirect_to("/support", :notice => 'Question was successfully created.') }
else
format.html { render :action => "new" }
end
end
end
# PUT /support/1
# PUT /support/1.xml
def update
@support_item = Support.find(params[:id])
respond_to do |format|
if @support_item.update_attributes(params[:support_item])
format.html { redirect_to("/", :notice => 'Question was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @support_item.errors, :status => :unprocessable_entity }
end
end
end
support.rb
class Support < ActiveRecord::Base
belongs_to :role
scope :admin_available, order("role_id ASC") do
Support.all
end
def self.available(user)
questions = where(:role_id => 1)
questions += where(:role_id => user.roles)
questions
end
end
类支持 <% if @support_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@support_item.errors.count, "error") %> prohibited this question from being saved:</h2>
<ul>
<% @support_item.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Support item for:" %><br />
<%= f.collection_select :role_id, Role.find_by_max(5), :id, :name, {:default => 'everyone'} %>
</div>
<div class="field">
<%= f.label :question %><br />
<%= f.text_field :question, :class => 'genForm_question'%>
</div>
<div class="field">
<%= f.label :answer %><br />
<%= f.text_area :answer, :class => 'genForm_textarea' %>
</div>
<div class="field">
<%= f.label :url %><br />
<%= f.text_field :url, :class => 'genForm_question' %>
</div>
<div class="actions">
<%= f.submit %>
</div>
禁止保存此问题:
“每个人”}%>
“genForm\u问题“%”
“genForm\u textarea%%>
“genForm\u问题“%”
new.html.erb
<h1>New Support Item</h1>
<% form_for @support_item, :url => { :action => "create" }, :html => { :method => :post } do |f| %>
<%= render 'form', :f => f %>
<% end %>
新的支持项
{:action=>“create”},:html=>{:method=>:post}do | f |%>
f%>
edit.html.erb
<h1>Editing Support Item</h1>
<% form_for @support_item, :url => { :action => "edit" }, :html => { :method => :post } do |f| %>
<%= render 'form', :f => f %>
<% end %>
编辑支持项
{:action=>“edit”},:html=>{:method=>:post}do | f |%>
f%>
我相信这就是所有相关代码。编辑支持项
<h1>Editing Support Item</h1>
<% form_for @support_item do |f| %>
<%= render 'form', :f => f %>
<% end %>
f%>
您正在覆盖URL。如果您使用标准rest做任何事情,它应该能够像那样自动生成。如果这不起作用,只需知道你不想提交到/support_items/1/edit,你想提交到/support_items/1。为什么在编辑表单中你要用POST动词发回编辑?你不应该用PUT动词发布“更新”吗?见下面@scragz的答案。只需将@post for new和edit的形式保留为_,rails解决了端点和动词的机制。问题不在我的代码中,我做了一些挖掘,发现rails保留了“支持”。我用“常见问题解答”代替“支持”创建了完全相同的东西,并且它无缝地工作。然后我所要做的就是在routes.rb文件中创建资源:faq,:path=>“support”,这很好。尽管如此,你仍然可以简化这些表格!你会发现Rails就是让Rails做它想做的事情。