0
我剛開始學習ruby on rails。我遵循http://edgeguides.rubyonrails.org/getting_started.html指南。PostsController中的語法錯誤ruby on rails上的#index
posts_controller.rb
文件包含以下代碼
class PostsController < ApplicationController
def create
@post = Post.new(post_params)
if @post.save
redirect_to @post
else
render 'new'
end
end
def show
@post = Post.find(params[:id])
end
def index
@posts = Post.all
end
def edit
@post = Post.find(params[:id])
end
def update
@post = Post.find(params[:id])
if @post.update(post_params)
redirect_to @post
else
render 'edit'
end
end
def destroy
@post = Post.find(params[:id])
@post.destroy
redirect_to posts_path
end
private
def post_params
params.require(:post).permit(:title, :text)
end
而且我app/views/posts/new.html.erb
文件包含
<h1>New post</h1>
<%= form_for :post, url: posts_path do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<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>
<p>
<%= f.submit %>
</p>
<% end %>
<%= form_for :post do |f| %>
...
<% end %>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
而且app/views/posts/edit.html.erb
包含
<h1>Edit post</h1>
<%= form_for :post, url: post_path(@post), method: :patch do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<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>
<p>
<%= f.submit %>
</p>
<% end %>
<%= render 'form' %>
<%= link_to 'Back', posts_path %>
SyntaxError in PostsController#index
C:/Users/punitha/blog/app/controllers/posts_controller.rb:50: syntax error, unexpected end-of-input, expecting keyword_end
Rails.root: C:/Users/punitha/blog
Application Trace | Framework Trace | Full Trace
Request
Parameters:
None
Toggle session dump
Toggle env dump
Response
Headers:
none.
最底部我們不需要任何視圖模板,錯誤是 - 在錯誤消息指出 - 在'posts_controller線50。 rb'。這個問題似乎是一個缺失的「結束」聲明。也許你發佈完整的控制器代碼 - 在粘貼的代碼中,一切似乎都很好。 –
請發佈完整的'posts_controller.rb'文件。 – Surya
你應該發佈整個'posts_controller.rb'文件,而不僅僅是類的內容。修正代碼中的縮進可能會讓您知道丟失的「end」在哪裏。 – csexton