没有路线匹配 [POST] “/tv_series/new”
No route matches [POST] "/tv_series/new"
当我显示表单时,我输入了一些内容然后我希望它进入索引视图
_form.html.erb
<%= simple_form_for @series, :url => { :action => :new } do |f| %>
<%= f.input :title, label: "Series Title" %>
<%= f.input :description %>
<%= f.input :actor %>
<%= f.button :submit %>
<% end %>
有人可以帮我找出错误吗?我不知道为什么当我尝试创建时它没有创建电视连续剧,当我检查 tvseries.connection
时,它说它 nil
,里面什么都没有。
tv_series_controller.rb
class TvSeriesController < ApplicationController
def index
end
def new
@series = Series.new
end
def create
@series = Series.new(serie_params)
if @series.save
redirect_to root_path
else
render 'new'
end
end
private
def show
params.require(:series).permit(:title, :description,:actor)
end
end
new.html.erb
<h1>New Serie</h1>
<%= render 'form' %>
routes.rb
Rails.application.routes.draw do
resources :tv_series
root 'tv_series#index'
end
_create_series.rb
class CreateSeries < ActiveRecord::Migration
def change
create_table :series do |t|
t.string :title
t.text :description
t.string :actor
t.timestamps null: false
end
end
end
删除 , :url => { :action => :new }
就可以了。
这是因为没有匹配POST /tv_series/new
的路线。相反,让 Rails 通过删除上面的行来为您找出路线,它将使用 POST /tv_series
.
如果你 运行 rake routes
你应该能够看到所有可用的路由及其相应的 HTTP 动词(GET、POST、PATCH 等)
在旁注中,我认为您的私有方法命名不正确。看起来你有 def show
而你想要的是 def serie_params
.
运行 rake routes 到控制台,您将获得不同的 HTTP 动词路径(GET、POST、PUT/PATCH、DELETE)
相应地使用路径。
在您创建 tv_series 的情况下,您必须使用 post HTTP 动词。
将 :url => { :action => :new } 替换为为 POST HTTP 动词提供的创建操作路径。它会很好地工作。
对于rails 4,默认情况下它具有相同的控制器、模型和视图名称。在这种情况下,您有不同的模型名称和 controller/view。所以你必须确定你的 URL 在你的表格中变成这样:
<%= simple_form_for @series, :url => { :controller => :tv_series, :action => :create }, :method => :post do |f| %>
或
<%= simple_form_for @series, :url => tv_series_path, :method => :post do |f| %>
希望对你有所帮助。
当我显示表单时,我输入了一些内容然后我希望它进入索引视图
_form.html.erb
<%= simple_form_for @series, :url => { :action => :new } do |f| %>
<%= f.input :title, label: "Series Title" %>
<%= f.input :description %>
<%= f.input :actor %>
<%= f.button :submit %>
<% end %>
有人可以帮我找出错误吗?我不知道为什么当我尝试创建时它没有创建电视连续剧,当我检查 tvseries.connection
时,它说它 nil
,里面什么都没有。
tv_series_controller.rb
class TvSeriesController < ApplicationController
def index
end
def new
@series = Series.new
end
def create
@series = Series.new(serie_params)
if @series.save
redirect_to root_path
else
render 'new'
end
end
private
def show
params.require(:series).permit(:title, :description,:actor)
end
end
new.html.erb
<h1>New Serie</h1>
<%= render 'form' %>
routes.rb
Rails.application.routes.draw do
resources :tv_series
root 'tv_series#index'
end
_create_series.rb
class CreateSeries < ActiveRecord::Migration
def change
create_table :series do |t|
t.string :title
t.text :description
t.string :actor
t.timestamps null: false
end
end
end
删除 , :url => { :action => :new }
就可以了。
这是因为没有匹配POST /tv_series/new
的路线。相反,让 Rails 通过删除上面的行来为您找出路线,它将使用 POST /tv_series
.
如果你 运行 rake routes
你应该能够看到所有可用的路由及其相应的 HTTP 动词(GET、POST、PATCH 等)
在旁注中,我认为您的私有方法命名不正确。看起来你有 def show
而你想要的是 def serie_params
.
运行 rake routes 到控制台,您将获得不同的 HTTP 动词路径(GET、POST、PUT/PATCH、DELETE)
相应地使用路径。
在您创建 tv_series 的情况下,您必须使用 post HTTP 动词。 将 :url => { :action => :new } 替换为为 POST HTTP 动词提供的创建操作路径。它会很好地工作。
对于rails 4,默认情况下它具有相同的控制器、模型和视图名称。在这种情况下,您有不同的模型名称和 controller/view。所以你必须确定你的 URL 在你的表格中变成这样:
<%= simple_form_for @series, :url => { :controller => :tv_series, :action => :create }, :method => :post do |f| %>
或
<%= simple_form_for @series, :url => tv_series_path, :method => :post do |f| %>
希望对你有所帮助。