Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.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 没有与[POST]匹配的路线”/故事/新“;提交表格后_Ruby_Ruby On Rails 3_Rails Routing - Fatal编程技术网

Ruby 没有与[POST]匹配的路线”/故事/新“;提交表格后

Ruby 没有与[POST]匹配的路线”/故事/新“;提交表格后,ruby,ruby-on-rails-3,rails-routing,Ruby,Ruby On Rails 3,Rails Routing,我刚刚开始“在Rails上构建自己的Ruby”,我不得不经常使用Google,因为这本书似乎有很多地方代码不起作用。这一次,我找不到答案。好吧,就这么定了。我有一个表单如下所示: FirstApp::Application.routes.draw do resources :story def new @story = Story.new(params[:story]) if request.post? @story.save end en

我刚刚开始“在Rails上构建自己的Ruby”,我不得不经常使用Google,因为这本书似乎有很多地方代码不起作用。这一次,我找不到答案。好吧,就这么定了。我有一个表单如下所示:

FirstApp::Application.routes.draw do
    resources :story 
  def new
    @story = Story.new(params[:story])
    if request.post?
      @story.save
    end
  end
def create
  @story = params[:story]
  if @story.save
    redirect_to @story, notice: "Story created"
  else
    render action: "new"
  end
end
new.html.erb

<%= form_for :story do |f| %>
<p>
    name:<br />
    <%= f.text_field :name %>
</p>

<p>
    link: <br />
    <%= f.text_field :link %>
</p>

<p>
    <%= submit_tag %>
</p>

<% end %>
我的
routes.rb
如下所示:

FirstApp::Application.routes.draw do
    resources :story 
  def new
    @story = Story.new(params[:story])
    if request.post?
      @story.save
    end
  end
def create
  @story = params[:story]
  if @story.save
    redirect_to @story, notice: "Story created"
  else
    render action: "new"
  end
end
story\u控制器
如下所示:

FirstApp::Application.routes.draw do
    resources :story 
  def new
    @story = Story.new(params[:story])
    if request.post?
      @story.save
    end
  end
def create
  @story = params[:story]
  if @story.save
    redirect_to @story, notice: "Story created"
  else
    render action: "new"
  end
end
story\u controller
new
内容是直接从书中摘录出来的。我想我可能有一个解决办法,但没有骰子。任何帮助都将不胜感激

我猜你的意思是(注意标志):

我猜你的意思是(注意标志):


首先,Rails在使用单数和复数名称时依赖于约定而不是配置。如果要遵循约定,必须将
routes.rb
中的行更改为
resources:stories
,这将生成以下路由:

   stories GET    /stories(.:format)          stories#index
           POST   /stories(.:format)          stories#create
 new_story GET    /stories/new(.:format)      stories#new
edit_story GET    /stories/:id/edit(.:format) stories#edit
     story GET    /stories/:id(.:format)      stories#show
           PUT    /stories/:id(.:format)      stories#update
           DELETE /stories/:id(.:format)      stories#destroy
story_index GET    /story(.:format)          story#index
            POST   /story(.:format)          story#create
  new_story GET    /story/new(.:format)      story#new
 edit_story GET    /story/:id/edit(.:format) story#edit
      story GET    /story/:id(.:format)      story#show
            PUT    /story/:id(.:format)      story#update
            DELETE /story/:id(.:format)      story#destroy
注意,在这种情况下,您必须将控制器重命名为
StoriesController
。但是,您的
routes.rb
具有
资源:story
,它生成以下路由:

   stories GET    /stories(.:format)          stories#index
           POST   /stories(.:format)          stories#create
 new_story GET    /stories/new(.:format)      stories#new
edit_story GET    /stories/:id/edit(.:format) stories#edit
     story GET    /stories/:id(.:format)      stories#show
           PUT    /stories/:id(.:format)      stories#update
           DELETE /stories/:id(.:format)      stories#destroy
story_index GET    /story(.:format)          story#index
            POST   /story(.:format)          story#create
  new_story GET    /story/new(.:format)      story#new
 edit_story GET    /story/:id/edit(.:format) story#edit
      story GET    /story/:id(.:format)      story#show
            PUT    /story/:id(.:format)      story#update
            DELETE /story/:id(.:format)      story#destroy
正如您所看到的,确实没有
POST/story/new
的路线。我猜,您得到的错误是由控制器中的以下代码触发的:

if request.post?
  @story.save
end
这是完全错误的,因为您试图在通过
GET
路由到的操作中检查
POST
请求。只需将此代码从您的
new
操作中删除,并将
create
操作添加到您的
StoryController
中,如下所示:

FirstApp::Application.routes.draw do
    resources :story 
  def new
    @story = Story.new(params[:story])
    if request.post?
      @story.save
    end
  end
def create
  @story = params[:story]
  if @story.save
    redirect_to @story, notice: "Story created"
  else
    render action: "new"
  end
end

这将暂时解决您的问题。但我强烈建议您在资源中使用复数的
故事
,因为它会再次困扰您。

首先,Rails在使用单数和复数名称时依赖于约定而不是配置。如果要遵循约定,必须将
routes.rb
中的行更改为
resources:stories
,这将生成以下路由:

   stories GET    /stories(.:format)          stories#index
           POST   /stories(.:format)          stories#create
 new_story GET    /stories/new(.:format)      stories#new
edit_story GET    /stories/:id/edit(.:format) stories#edit
     story GET    /stories/:id(.:format)      stories#show
           PUT    /stories/:id(.:format)      stories#update
           DELETE /stories/:id(.:format)      stories#destroy
story_index GET    /story(.:format)          story#index
            POST   /story(.:format)          story#create
  new_story GET    /story/new(.:format)      story#new
 edit_story GET    /story/:id/edit(.:format) story#edit
      story GET    /story/:id(.:format)      story#show
            PUT    /story/:id(.:format)      story#update
            DELETE /story/:id(.:format)      story#destroy
注意,在这种情况下,您必须将控制器重命名为
StoriesController
。但是,您的
routes.rb
具有
资源:story
,它生成以下路由:

   stories GET    /stories(.:format)          stories#index
           POST   /stories(.:format)          stories#create
 new_story GET    /stories/new(.:format)      stories#new
edit_story GET    /stories/:id/edit(.:format) stories#edit
     story GET    /stories/:id(.:format)      stories#show
           PUT    /stories/:id(.:format)      stories#update
           DELETE /stories/:id(.:format)      stories#destroy
story_index GET    /story(.:format)          story#index
            POST   /story(.:format)          story#create
  new_story GET    /story/new(.:format)      story#new
 edit_story GET    /story/:id/edit(.:format) story#edit
      story GET    /story/:id(.:format)      story#show
            PUT    /story/:id(.:format)      story#update
            DELETE /story/:id(.:format)      story#destroy
正如您所看到的,确实没有
POST/story/new
的路线。我猜,您得到的错误是由控制器中的以下代码触发的:

if request.post?
  @story.save
end
这是完全错误的,因为您试图在通过
GET
路由到的操作中检查
POST
请求。只需将此代码从您的
new
操作中删除,并将
create
操作添加到您的
StoryController
中,如下所示:

FirstApp::Application.routes.draw do
    resources :story 
  def new
    @story = Story.new(params[:story])
    if request.post?
      @story.save
    end
  end
def create
  @story = params[:story]
  if @story.save
    redirect_to @story, notice: "Story created"
  else
    render action: "new"
  end
end
这将暂时解决您的问题。但我强烈建议您使用复数
故事作为参考,因为它会再次困扰您。

这是您(和我)在指南中错过的部分:

但是这个表单有一个问题。如果您检查HTML 这是通过查看页面的源代码生成的,您将看到 表单的action属性指向/articles/new。 这是一个问题,因为这条路线会到达您正在访问的页面 此时在右侧,该路线应仅用于显示 新文章的形式

表单需要使用不同的URL才能转到其他地方。 只需使用表单_for的:url选项即可完成此操作。 通常在Rails中,用于提交新表单的操作 这样就叫做“创建”,所以表单应该指向 这一行动

编辑app/views/articles/new.html.erb内部行的表单,如下所示:



这是您(和我)在指南中遗漏的部分:

但是这个表单有一个问题。如果您检查HTML 这是通过查看页面的源代码生成的,您将看到 表单的action属性指向/articles/new。 这是一个问题,因为这条路线会到达您正在访问的页面 此时在右侧,该路线应仅用于显示 新文章的形式

表单需要使用不同的URL才能转到其他地方。 只需使用表单_for的:url选项即可完成此操作。 通常在Rails中,用于提交新表单的操作 这样就叫做“创建”,所以表单应该指向 这一行动

编辑app/views/articles/new.html.erb内部行的表单,如下所示:




您的控制器代码错误。Rails遵循CRUD和Restful实践。一个新的操作总是一个get请求,并且代码与创建操作(即post)中的新建操作类似。您的控制器代码是错误的。Rails遵循CRUD和Restful实践。一个新的操作总是一个get请求,代码类似于你的新操作,应该在一个创建操作中,这是一个帖子;我直到现在才看到这个。嗯。当我将:story改为@story时,我得到了这个:未定义的方法‘stories_path’,根据书中的说法,它是:story,因为……Rails就是这样设计的。所以,只是因为,基本上,我想这不是最好的答案。虽然是关于create操作,但基本上是@story=story.new(params[:story])吗?这可能是一个超级愚蠢的问题,但是一个虚拟模型基本上是一个新的def然后结束,还是我应该添加一些其他东西?@user1447477-不用担心。但它肯定应该是
@story
,而不是
:story
(您可以查看)。如果这对你不起作用,我怀疑这是因为
@story
没有正确设置。我添加了一些示例控制器代码,可能会