Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UrlGenerationError on section 5.12 of Rails Guide

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
like image 603
brimtown Avatar asked Jul 05 '13 03:07

brimtown


1 Answers

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>
like image 124
Henry Avatar answered Sep 19 '22 03:09

Henry