未定义方法 `articles_path’ for #

huangapple go评论108阅读模式
英文:

undefined method `articles_path' for #<ActionView::Base:0x0000000000b450>

问题

以下是翻译好的内容:

这是名为Articles的控制器

  1. def new
  2. @article = Article.new
  3. end
  4. def show
  5. @article = Article.find(params[:id])
  6. end
  7. def create
  8. @article = Article.new(article_params)
  9. if @article.save
  10. redirect_to @article
  11. else
  12. render :new, status: :unprocessable_entity
  13. end
  14. end

这是视图

  1. <h1>New Article</h1>
  2. <%= form_for(@article) do |form| %>
  3. <div>
  4. <%= form.label :title %><br>
  5. <%= form.text_field :title %>
  6. </div>
  7. <div>
  8. <%= form.label :body %><br>
  9. <%= form.text_field :body %>
  10. </div>
  11. <div>
  12. <%= form.submit %>
  13. </div>
  14. <% end %>

这是路由

  1. get 'articles/new', to: 'articles#new'
  2. post 'articles/create', to: 'articles#create'

当我尝试访问路由 <http://localhost:3000/articles/new> 时,出现错误:undefined method `articles_path' for #ActionView::Base:0x0000000000b450

英文:

This is the controller named Articles

  1. def new
  2. @article =Article.new
  3. end
  4. def show
  5. @article=Article.find(params[:id])
  6. end
  7. def create
  8. @article=Article.new(article_params)
  9. if @article.save
  10. redirect_to @article
  11. else
  12. render :new, status: :unprocessable_entity
  13. end
  14. end

This is the view

  1. &lt;h1&gt;New Article&lt;/h1&gt;
  2. &lt;%form_for(@article) do |form|%&gt;
  3. &lt;div&gt;
  4. &lt;%=form.label :title%&gt;&lt;br&gt;
  5. &lt;%=form.text_field :title%&gt;
  6. &lt;/div&gt;
  7. &lt;div&gt;
  8. &lt;%=form.label :body%&gt;&lt;br&gt;
  9. &lt;%=form.text_field :body%&gt;
  10. &lt;/div&gt;
  11. &lt;div&gt;
  12. &lt;%=form.submit%&gt;
  13. &lt;/div&gt;
  14. &lt;%end%&gt;

This is the routes

  1. get &#39;articles/new&#39;, to: &#39;articles#new&#39;
  2. post &#39;articles/create&#39;, to: &#39;articles#create&#39;

when I want to access the route <http://localhost:3000/articles/new> it gives error undefined method `articles_path' for #&lt;ActionView::Base:0x0000000000b450&gt;

答案1

得分: 1

错误发生在form_for中。当提供新的article对象时,它必须猜测表单将提交到的路径。按照惯例,它尝试执行&quot;#{object.model_name.plural}_path&quot;

您的路由没有名称,也不生成辅助方法。您可以使用以下方式显式地为它们命名:

  1. get 'articles/new', to: 'articles#new', as: :new_article
  2. post 'articles/create', to: 'articles#create', as: :articles

或者使用resources快捷方式:

  1. resources :articles, only: %i[new create]
英文:

The error is raised within form_for. When new article object is given, it has to guess the path the form will be submitted to. Following convention, it tries to execute &quot;#{object.model_name.plural}_path&quot;

Your routes has no names and do not generate helpers. You can give them names explicitly with:

  1. get &#39;articles/new&#39;, to: &#39;articles#new&#39;, as: :new_article
  2. post &#39;articles/create&#39;, to: &#39;articles#create&#39;, as: :articles

Or use resources shortcut instead:

  1. resources :articles, only: %i[new create]

huangapple
  • 本文由 发表于 2023年7月23日 14:25:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76746884.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定