Rails 3 - 表单提交问题

时间:2011-03-27 19:15:04

标签: ruby-on-rails

我无法从show.html.erb页面获取提交内容。它是一个嵌套的表单,从/ event / 1 / ticketbuilder / 1发送到同一页面,但是,当我提交表单时,Ig等路由错误(无路由匹配“/ event / 1 / ticketbuilder / 1”)甚至虽然直接去那个网址工作得很好。

#show.html.erb
<%= form_for @section, :url => event_ticketbuilder_path(@event) do |s| %>
  <%= s.text_field(:name) %> <%= submit_tag("Add Section") %>
<% end %>

#ticketbuilder_controller.rb
class TicketbuilderController < ApplicationController
 def show
    @event = Event.find(params[:event_id])
    @section = Section.new
  end
  def create
    @event = Event.new(params[:event_id])
    @section = @event.sections.build(params[:name])
    if @section.save
      @section = Section.new
    end
    render :action => :show
  end
end

直接链接到页面时,它成功了,我得到了

Started GET "/event/1/ticketbuilder/1" for 206.248.211.83 at Sun Mar 27 15:02:24 -0400 2011
  Processing by TicketbuilderController#show as HTML
  Parameters: {"event_id"=>"1", "id"=>"1"}
  Event Load (0.3ms)  SELECT "events".* FROM "events" WHERE "events"."id" = 1 LIMIT 1
  Section Load (0.3ms)  SELECT "sections".* FROM "sections" WHERE ("sections".event_id = 1)
  Location Load (0.3ms)  SELECT "locations".* FROM "locations" WHERE ("locations".section_id = 1)
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Rendered ticketbuilder/show.html.erb within layouts/application (52.2ms)
Completed 200 OK in 85ms (Views: 56.8ms | ActiveRecord: 1.4ms)

在该页面上提交表单时,我

Started POST "/event/1/ticketbuilder/1" for 206.248.211.83 at Sun Mar 27 15:02:26 -0400 2011

ActionController::RoutingError (No route matches "/event/1/ticketbuilder/1"):


Rendered /usr/lib/ruby/gems/1.8/gems/actionpack-3.0.5/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within rescues/layout (1.0ms)

似乎我可以使用“GET”方法访问页面而不是“POST”我认为这与通过URL发送的变量有关,但我目前对rails没有足够的了解解决这个问题。

event_ticketbuilder_index GET    /event/:event_id/ticketbuilder(.:format)          {:controller=>"ticketbuilder", :action=>"index"}
                          POST   /event/:event_id/ticketbuilder(.:format)          {:controller=>"ticketbuilder", :action=>"create"}
  new_event_ticketbuilder GET    /event/:event_id/ticketbuilder/new(.:format)      {:controller=>"ticketbuilder", :action=>"new"}
 edit_event_ticketbuilder GET    /event/:event_id/ticketbuilder/:id/edit(.:format) {:controller=>"ticketbuilder", :action=>"edit"}
      event_ticketbuilder GET    /event/:event_id/ticketbuilder/:id(.:format)      {:controller=>"ticketbuilder", :action=>"show"}
                          PUT    /event/:event_id/ticketbuilder/:id(.:format)      {:controller=>"ticketbuilder", :action=>"update"}
                          DELETE /event/:event_id/ticketbuilder/:id(.:format)      {:controller=>"ticketbuilder", :action=>"destroy"}
              event_index GET    /event(.:format)                                  {:controller=>"event", :action=>"index"}
                          POST   /event(.:format)                                  {:controller=>"event", :action=>"create"}
                          GET    /event/new(.:format)                              {:controller=>"event", :action=>"new"}
                          GET    /event/:id/edit(.:format)                         {:controller=>"event", :action=>"edit"}
                          GET    /event/:id(.:format)                              {:controller=>"event", :action=>"show"}
                          PUT    /event/:id(.:format)                              {:controller=>"event", :action=>"update"}
                          DELETE /event/:id(.:format)                              {:controller=>"event", :action=>"destroy"}

任何帮助或想法都会非常感激

1 个答案:

答案 0 :(得分:1)

尝试使用event_ticketbuilder_index_path:url。您需要创建操作,该操作尚未具有现有ID。