【问题标题】:UrlGenerationError on section 5.12 of Rails GuideRails 指南第 5.12 节上的 UrlGenerationError
【发布时间】:2013-07-05 03:07:50
【问题描述】:

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

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

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, url: post_path(@post.id) },
  method: :patch do |f| %>

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

posts_controller.rb:

类 PostsController

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>

路线:rb:

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4


    【解决方案1】:

    它说错误是由这一行引起的

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

    应该是

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

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

    【讨论】:

    • 而另一个只是}post_path(@post.id)之后的一个流浪者。
    • 谢谢,解决了。知道为什么该代码包含在指南中吗?我从网站上逐字复制。
    • @brimtown,你是对的。我检查指南。但是我还没有达到rails 4。我知道在 rails 4 之前,这是一个错误的表达方式。所以它可能是新的 Rails 方式,也可能是一个错字。但是由于您尝试了该示例并遇到了错误,因此很可能是他们的拼写错误。
    • @Henry,我在 Rails 4 上尝试过,但遇到了同样的问题。所以看起来这是一个错字!
    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 2010-12-30
    • 2015-11-16
    • 1970-01-01
    • 2011-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多