Why do the rails give me a SyntaxError index in PostsController # when I try to add a second model after the getting started guide for ruby ​​on rails

I am new to programming and rails. Please be kind. I followed the search for guidebooks on how to create my first blog http://guides.rubyonrails.org/getting_started.html

Im up to the 6th part where I have to add a comment model to the publication model. Following the instructions, I get this error.

    SyntaxError in PostsController#index
    /home/nadia/blog/app/models/post.rb:5: syntax error, unexpected ..., expecting ']' [...] 

    Extracted source (around line #4):

    def index 
     @post = Post.all
    end

This is my routes.rb

   1 Blog::Application.routes.draw do
   2
   3   
   5
   6   
   7   resources :posts
   8
   9   # You can have the root of your site routed with "root"
   10   root 'welcome#index'
   11
   12
   13   resources :posts do
   14       resources :comments
   15   end

this is my post_controller.rb

     1 class PostsController < ApplicationController
     2
     3   def index
     4     @post = Post.all 
     5   end
     6
     7   def new
     8     @post = Post.new
     9   end
     10   def create
     11     @ post = Post.new(params[:post].permit(:title, :text))
     12
     13     if @post.save
     14       redirect_to @post
     15     else
     16       render 'new'
     17     end
     18   end
     19
     20   def edit
     21     @post = Post.find(params[:id])
     22   end
     23
     24   def update
     25     @post = Post.find(params[:id])
     26
     27     if @post.update(params[:post].permit(:title, :text))
     28       redirect_to @post
     29     else
     30       render 'edit'
     31    end
     32   end
     33 
     34   def destroy
     35     @post = Post.find(params[:id])
     36     @post.destroy
     37
     38     redirect_to posts_path
     39   end
     40
     41   def show
     42     @post = Post.find(params[:id])
     43   end
     44
     45   private
     46  def post_params
     47       params.require(:post).permit(:title, :text)
     48     end
     49 end

comment_controller.rb

    1 class CommentsController < ApplicationController
    2   def create
    3     @post = Post.find(params[:post_id])
    4     @comment = @post.comments.create(params[:comment].permit(:commenter,     :body))
    5     redirect_to post_path(@post)
    6   end
    7 end

and my index.html.erb

    1 <h1>Listing posts</h1>
    2 <%= link_to 'New post', new_post_path %>
    3 <table>
    4   <tr>
    5     <th>Title</th>
    6     <th>Text</th>
    7     <th></th>
    8   </tr>
    9 
    10
    11   <% @post.each do |post| %>
    12     <tr>
    13         <td><%= post.title %></td>
    14         <td><%= post.text %></td>
    15         <td><%= link_to 'Show', post %></td>
    16         <td><%= link_to 'Edit', edit_post_path(post) %></td>
    17         <td><%= link_to 'Destroy', post_path(post), method: :delete, data    : { confirm: 'Are you sure?'} %> </td>
    18     </tr>
    19    <% end %>
    20 </table>

Please help, please let me know if I can provide additional information to help you help me.

+3
source share
3

@post, @posts. 11 index.html.erb. @posts - . @post .

+2

- app/models/post.rb, . , .

0

Yeah. I get it. Thank. I changed the code of the four lines of the controller to

    @posts = Post.all

and then changed the index.html.erb file from

    <% @post.each do |post| %>

to

    <% @posts.each do |post| %>

I fixed the problem. Although I do not understand enough about the ruby, how HOW I did it. I know that black magic and BAM have occurred. Thanks to all.

-1
source

All Articles