Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么此表单在给我输入数据的机会之前会重定向我?_Ruby On Rails_Forms - Fatal编程技术网

Ruby on rails 为什么此表单在给我输入数据的机会之前会重定向我?

Ruby on rails 为什么此表单在给我输入数据的机会之前会重定向我?,ruby-on-rails,forms,Ruby On Rails,Forms,Rails控制器: class VenuesController < ApplicationController def new @venue = Venue.new end def create @venue = Venue.new(params[:venue]) if @venue.save redirect_to root_path end end def update redirect_to search

Rails控制器:

class VenuesController < ApplicationController

  def new
    @venue = Venue.new
  end

  def create
    @venue = Venue.new(params[:venue])
    if @venue.save
      redirect_to root_path
    end
  end

  def update
    redirect_to search_path
  end

end
classvenuescoontroller
轨道形式:

<%= form_for(@venue) do |f| %>

  <%= f.text_field :foursquare_id %>

  <%= f.submit "Save" %>

<% end %>

“foursquare_id”是“场馆”表中的一列。通常我会从foursquare导入一个foursquare id,但我会出于测试目的键入文本。在我有机会输入表单之前,我被重定向到“根路径”


我的控制器/表单缺少什么?提前谢谢

模板表单应该由
new
操作使用,
new.html.erb
作为文件名。你应该去
/viouses/new
填写表格

create
操作用于提交完成的表单,这就是您被重定向的原因。您还应修改
create
,以处理无法保存的模型:

def create
  @venue = Venue.new(params[:venue])
  if @venue.save
    redirect_to root_path
  else
    render :action => :new
  end
end
或者,您也可以使用速记:

def create
  @venue = Venue.new(params[:venue])
  @venue.save
  respond_with @venue, :location => root_path
end

您用于创建新场馆的表单应位于Vinces/new.html.erb中,该表单将在表单提交时在控制器中调用创建操作。在这种情况下,您不应该创建视图

这是在修改或创建(或两者)时发生的吗?表单的视图模板是否位于正确的位置和正确的名称?您是否有机会看到表单?如果是,那么您是否在视图中使用了任何类型的js?在实际键入内容之前是否在输入中按ENTER键?在创建中。我的视图模板(我相信)放在了正确的位置(viouses/create.html.erb)。我想我应该在这一点上找到Rails表单了……谢谢!是否有一个关于Rails表单的特别好的参考资料,或者你只需要看看guides.rubyonrails.org?除了指南(强烈推荐)之外,这是一个很好的资源。还有一个由迈克尔·哈特尔编写的流行教程——谷歌是你的朋友。