Rails 5入门教程,没有路由匹配[POST]" / articles / new",

时间:2016-08-29 23:38:01

标签: ruby-on-rails post routes ruby-on-rails-5

我正在学习本教程:Rails入门http://guides.rubyonrails.org/getting_started.html

我在堆栈上搜索过:没有路由匹配[POST]“/ articles / new”No route matches [POST] "/articles/new",推荐的拼写更正对我的错误没有帮助。

你可以找到我的git:https://github.com/tomile/rails5Blog/tree/adding-partial

的routes.rb

Rails.application.routes.draw do
  resources :articles
  get 'welcome/index'
  root 'welcome#index'
end

articles_controller.rb

def create
    @article = Article.new(article_params)

    if @article.save
        redirect_to @article
    else
        render 'new'
    end
end

_form.html.erb(没有复制粘贴整个文件)

<%= form_for :article do |f| %>

...

  <p>
    <%= f.submit %>
  </p>
<% end %>

new.html.erb

<h1>New Article</h1>

<%= render 'form' %>

<%= link_to 'Back', articles_path %>

$ rake routes

           Prefix Verb   URI Pattern                  Controller#Action
         articles GET    /articles(.:format)          articles#index
                  POST   /articles(.:format)          articles#create
      new_article GET    /articles/new(.:format)      articles#new
                  POST   /articles(.:format)          articles#create
      new_article GET    /articles/new(.:format)      articles#new
     edit_article GET    /articles/:id/edit(.:format) articles#edit
          article GET    /articles/:id(.:format)      articles#show
                  PATCH  /articles/:id(.:format)      articles#update
                  PUT    /articles/:id(.:format)      articles#update
                  DELETE /articles/:id(.:format)      articles#destroy

2 个答案:

答案 0 :(得分:0)

新路线的第一件事是GET型。 第二件事是,当我们使用form_for时,我们为它提供了一个实例。

所以在你的文章控制器中

minim_state_TRUE_fit<-GenSA(par=params,fn=function(params, ...) {     
                                                minim(params[1],params[2], ...) }, 
                            lower=lower_limits, upper=upper_limits, 
                            control=list(max.time=100), state="TRUE")

然后以表格形式使用

def new
  @article = Article.new
end

它将指向表单操作以创建文章方法。

答案 1 :(得分:0)

您错过了教程的一个步骤。 您必须将表单的第一行更改为以下内容:

<%= form_for :article, url: articles_path do |f| %>

在该表单正常工作之后。

相关问题