Rails 指南第 5.12 节中的 UrlGenerationError [英] UrlGenerationError on section 5.12 of Rails Guide

查看:51
本文介绍了Rails 指南第 5.12 节中的 UrlGenerationError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注位于此处的 Rails 4.0.0 入门教程:http://guides.rubyonrails.org/getting_started.html#updating-posts

当我尝试从主页 (localhost) 导航到 localhost/posts 时,出现此错误:

ActionController::UrlGenerationError in Posts#index显示 C:/RailsInstaller/blog/app/views/posts/index.html.erb 其中第 16 行提出:没有路由匹配 {:action=>"show", :controller=>"posts"} 缺少必需的键:[:id]提取的源代码(围绕第 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<%结束%>

此外,当我尝试编辑帖子时,在我的 edit.html.erb 文件中收到一个 SyntaxError:

C:/RailsInstaller/blog/app/views/posts/edit.html.erb:3: 语法错误,意外的'}',需要keyword_end';@output_buffer.append= form_for :post, url: post_path(@post.id) },^C:/RailsInstaller/blog/app/views/posts/edit.html.erb:32: 语法错误,意外的keyword_ensure,期待$end

具体来说,用这一行:

<%= form_for :post, url: post_path(@post.id) },方法: :patch do |f|%>

这是我第一次尝试学习 Rails,所以当我遇到这些错误时,我很难理解从哪里开始寻找.以下是一些相关文件:

posts_controller.rb:

class PostsController

应用控制器

def 索引@posts = Post.all结尾定义新@post = Post.new结尾定义创建@post = Post.new(params[:post].permit(:title, :text))如果@post.save重定向到@post别的呈现新"结尾结尾高清秀@post = Post.find(params[:id])结尾定义更新@post = Post.find(params[:id])如果@post.update(params[:post].permit(:title, :text))重定向到@post别的渲染编辑"结尾结尾定义编辑@post = Post.find(params[:id])结尾私人的def post_paramsparams.require(:post).permit(:title, :text)结尾结尾

edit.html.rb:

编辑帖子

<%= form_for :post, url: post_path(@post.id) },方法: :patch do |f|%><% 如果@post.errors.any?%><div id="错误解释"><h2><%=pluralize(@post.errors.count, "error") %>禁止此帖子已被保存:</h2><ul><% @post.errors.full_messages.each 做 |msg|%><li><%=msg%></li><%结束%>

<%结束%><p><%= f.label :title %><br><%= f.text_field :title %></p><p><%= f.label :text %><br><%= f.text_area :text %></p>

<%= f.submit %>

路线:RB:

Blog::Application.routes.draw 做资源:帖子根到:欢迎#index"结尾

解决方案

说错误是由这一行引起的

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

应该是

<%= link_to 'Show', post %>

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

I'm following the Getting Started tutorial for Rails 4.0.0 located here: http://guides.rubyonrails.org/getting_started.html#updating-posts

When I try to navigate from the main page (localhost) to localhost/posts, I am getting this error:

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 %>

Additionally, when I try to edit a post, I receive a SyntaxError in my edit.html.erb file:

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

Specifically, with this line:

<%= form_for :post, url: post_path(@post.id) },
  method: :patch do |f| %>

This my first attempt at learning Rails, so I'm having a difficult time understanding where to even begin to look when I'm getting these errors. Here are some relevant files:

posts_controller.rb:

class PostsController < ApplicationController

def index
  @posts = Post.all
end

def new
  @post = Post.new
end

def create
  @post = Post.new(params[:post].permit(:title, :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(:title, :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(:title, :text)
  end
end

edit.html.rb:

<h1>Editing post</h1>

<%= form_for :post, url: post_path(@post.id) },
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 :title %><br>
    <%= f.text_field :title %>
  </p>

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

<%= f.submit %>

routes:rb:

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

解决方案

it says the error is caused by this line

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

It should be

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

or

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

这篇关于Rails 指南第 5.12 节中的 UrlGenerationError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆