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>

投稿#新規

<h1>New Post</h1>

<%= render 'form' %>
4

1 に答える 1

1

update アクションで「if」が抜けています。

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 に答える