2010-09-13 40 views
0

我是Rails的新手,但多年來廣泛使用PHP。我正在構建一個簡單的博客(我知道),以在MVC/Rails世界中獲得我的技能。在軌道中使用Maruku的問題3

我有基本的工作,但花了週末試圖讓Maruku工作,例如一個文本區域的Markdown Extra markup保存到數據庫,然後再回到瀏覽器。

我用下面的代碼在我的崗位模型,但我得到一個錯誤,當我嘗試加載/帖 - 「未定義的局部變量或方法`maruku」爲#」

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 

我也嘗試過類似的東西在我的帖子控制器,我在這裏找到。然後叫@ 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 

回答

1

你需要使用(注意大小寫):在紅寶石

Maruku.new(...) 

常量以大寫字母開頭,變量的開始小寫字母(你正在訪問一個類,這是一個常量)。

此外,請確保您在Gemfile中包含gem(Rails 3要求在此文件中指定所有庫)。

最後,您不能像列出的那樣使用Maruku。相反,嘗試:

class Post < ActiveRecord::Base 

    ... 

    def content_html 
     Maruku.new(self.content).to_html  
    end 

end 

然後在您的視圖中,可以通過<%= @ post.content_html%>訪問。請注意,您應該使用回調(請參閱Active Record Callbacks)對HTML進行轉換,以提高某些時候的性能,但這會讓您啓動並運行。

+0

非常感謝幫助 - 沒有意識到這一點。我做了更改,但現在在下一行中顯示「PostsController#index中有一個NoMethodError」錯誤,並提示需要使用「private method'gsub:content:Symbol」 – rollbahn 2010-09-13 03:18:06

+0

@rollbahn更新您的示例代碼。 – 2010-09-13 03:20:43

+0

不知道如何更新評論中的代碼,因爲它似乎沒有正確解析? class Post true validates:title,:presence => true, :length => {:minimum => 5} validates:content,:presence => true 驗證:摘錄:存在=>真 的has_many:評論:依賴=>:摧毀 Maruku.new(:含量).to_html 結束 – rollbahn 2010-09-13 04:30:25