0

首先,我是红宝石的新手。我正在从头开始建立一个论坛以获取乐趣。

完成授权工作,并且可以发布带有原始内容的新主题,但我对帖子(回复)控制器有问题。

这是我得到的错误:

SyntaxError in PostsController#new

/Users/mlegacy/Documents/RubyProjects/forum/app/controllers/posts_controller.rb:48: syntax 
error, unexpected keyword_end, expecting $end

这是我的控制器代码:

class PostsController < ApplicationController

  def index
    @posts = Post.order("sticky desc")
  end

  def show
    @post = Post.find(params[:id])
  end

  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    @post.user = current_user
  if @post.save
    redirect_to topics_url, notice: "Post created."
  else
    render :new
  end
end

def edit
  @post = Post.find(params[:id])
end

def update
  @post = Post.find(params[:id])
    redirect_to topics_url, notice: "Updated post."
  else
    render :edit
  end
end

def destroy
  @post = Post.find(params[:id])
  @post.destroy
  redirect_to topics_url, notics: "Post removed."
end

private

  def post_params
    params.require(:posts).permit(:content, :created_at, :updated_at)
  end
end

我的模型:

class Post < ActiveRecord::Base
  belongs_to :topic
end

然后是 topic#show 视图和 posts#new 视图:

主题#显示:

<h1><%= @topic.name %></h1>
<p><%= @topic.post_content %></p>

<% @topic.posts.each do |post| %>
  <div class="post">
    <%= post.content %>
  </div>
<% end %>

<p><%= link_to "Post new reply", posts_new_path %></p>

<p><%= link_to "Back to topics", topics_path %></p>

帖子#new

<h1>New Post</h1>

<%= render 'form' %>
4

1 回答 1

1

您在更新操作中错过了“如果”。

def update
  if @post = Post.find(params[:id])
    redirect_to topics_url, notice: "Updated post."
  else
    render :edit
  end
end
于 2013-07-06T18:42:17.730 回答