Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – “Rails指南”第5.12节上的UrlGenerationError大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在关注Rails 4.0.0入门教程,位于这里: http://guides.rubyonrails.org/getting_started.html#updating-posts

当我尝试从主页面(localhost)导航到本地主机/帖子时,我收到这个错误:

ActionController::UrlGenerationError in Posts#index
Showing C:/RailsInstaller/blog/app/views/posts/index.html.erb where line #16 raised:

No route matches {:action=>"show",:controller=>"posts"} missing required keys: [:id]
Extracted source (around line #16):

13   <tr>
14     <td><%= post.title %></td>
15     <td><%= post.text %></td>
16     <td><%= link_to 'Show',post_path %></td>
17     <td><%= link_to 'Edit',edit_post_path(post) %></td>
18   </tr>
19 <% end %>

另外,当我尝试编辑一篇文章时,我的edit.html.erb文件中收到一个SyntaxError:

C:/RailsInstaller/blog/app/views/posts/edit.html.erb:3: syntax error,unexpected '}',expecTing keyword_end
';@outpuT_Buffer.append=  form_for :post,url: post_path(@post.id) },^
C:/RailsInstaller/blog/app/views/posts/edit.html.erb:32: syntax error,unexpected keyword_ensure,expecTing $end

具体来说,用这一行:

<%= form_for :post,method: :patch do |f| %>

这是我第一次学习Rails的尝试,所以我很难理解当我收到这些错误时甚至开始看.以下是一些相关文件:

posts_controller.rb:

类帖子控件< ApplicationController的

def index
  @posts = Post.all
end

def new
  @post = Post.new
end

def create
  @post = Post.new(params[:post].permit(@R_384_6964@,:text))

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

def show
  @post = Post.find(params[:id])
end

def update
  @post = Post.find(params[:id])

  if @post.update(params[:post].permit(@R_384_6964@,:text))
    redirect_to @post
  else
    render 'edit'
  end
end

def edit
  @post = Post.find(params[:id])
end

private
  def post_params
    params.require(:post).permit(@R_384_6964@,:text)
  end
end

edit.html.rb:

<h1>EdiTing post</h1>

<%= form_for :post,method: :patch do |f| %>
  <% if @post.errors.any? %>
  <div id="errorExplanation">
    <h2><%= pluralize(@post.errors.count,"error") %> prohibited
      this post from being saved:</h2>
    <ul>
    <% @post.errors.full_messages.each do |msg| %>
      <li><%= msg %></li>
    <% end %>
    </ul>
  </div>
  <% end %>
  <p>
    <%= f.label @R_384_6964@ %><br>
    <%= f.text_field @R_384_6964@ %>
  </p>

  <p>
    <%= f.label :text %><br>
    <%= f.text_area :text %>
  </p>

<%= f.submit%>

路线:RB:

Blog::Application.routes.draw do
  resources :posts
  root to: "welcome#index"
end

解决方法

它说错误是由这一行引起的
<td><%= link_to 'Show',post_path %></td>

它应该是

<td><%= link_to 'Show',post %></td>

要么

<td><%= link_to 'Show',post_path(post) %></td>

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – “Rails指南”第5.12节上的UrlGenerationError全部内容,希望文章能够帮你解决ruby-on-rails – “Rails指南”第5.12节上的UrlGenerationError所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。