我是 Rails 新手,但多年来广泛使用 PHP。我正在构建一个简单的博客(我知道)来提高我在 MVC/Rails 世界中的技能。
我有基本的工作,但花了周末试图让 Maruku 工作,例如从带有 Markdown Extra 标记的文本区域保存的帖子正文到数据库,然后再返回到浏览器。
我在我的 Post 模型中使用了以下代码,但是当我尝试加载 /posts 时出现错误 - “未定义的局部变量或方法 `maruku' for #”
class Post < ActiveRecord::Base
validates :name, :presence => true
validates :title, :presence => true,
:length => { :minimum => 5 }
validates :content, :presence => true
validates :excerpt, :presence => true
has_many :comments, :dependent => :destroy
maruku.new(:content).to_html
end
我还在我在这里找到的帖子控制器中尝试了类似的东西。然后在我的 Show 视图中调用 @post.content 但得到一个错误:
body = maruku.new(post.body)
post.body = body.to_html
我很确定这是我的菜鸟大脑死了,但是任何帮助或指导都会很棒,因为我已经为此奋斗了两天。顺便说一句,我正在使用 maruku,因为我需要 Markdown Extra,因为我的旧博客文章都是这样格式化的。
谢谢
更新 - PostsController
class PostsController < ApplicationController
# GET /posts
# GET /posts.xml
def index
@posts = Post.find(:all, :order => 'created_at DESC')
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @posts }
end
end
# GET /posts/1
# GET /posts/1.xml
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/new
# GET /posts/new.xml
def new
@post = Post.new
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @post }
end
end
# GET /posts/1/edit
def edit
@post = Post.find(params[:id])
end
# POST /posts
# POST /posts.xml
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save
format.html { redirect_to(@post, :notice => 'Post was successfully created.') }
format.xml { render :xml => @post, :status => :created, :location => @post }
else
format.html { render :action => "new" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# PUT /posts/1
# PUT /posts/1.xml
def update
@post = Post.find(params[:id])
respond_to do |format|
if @post.update_attributes(params[:post])
format.html { redirect_to(@post, :notice => 'Post was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @post.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.xml
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
end