首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Rails 3中使用Maruku时出现的问题

在Rails 3中使用Maruku时出现的问题
EN

Stack Overflow用户
提问于 2010-09-13 10:42:01
回答 1查看 784关注 0票数 0

我是Rails的新手,但多年来一直广泛使用PHP。我正在建立一个简单的博客(我知道)来提升我在MVC/Rails世界中的技能。

我有基本的工作,但我花了一个周末试图让Maruku工作,例如,一个从文本区域保存的帖子主体,带有Markdown额外的标记到数据库,然后再返回到浏览器。

我在我的Post模型中使用了以下代码,但在尝试加载/posts时出现错误-“未定义的局部变量或#的方法‘`maruku’”

代码语言:javascript
复制
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,但得到了一个错误:

代码语言:javascript
复制
body = maruku.new(post.body)
post.body = body.to_html

我非常确定是我的新手大脑死了,但任何帮助或指导都是很好的,因为我已经和这个问题斗争了两天。顺便说一句,我正在使用maruku,因为我需要额外的Markdown,因为我的旧博客帖子都是这样格式的。

谢谢

更新- PostsController

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2010-09-13 10:54:29

您需要使用(注意用例):

代码语言:javascript
复制
Maruku.new(...)

ruby中的常量以大写字母开头,变量以小写字母开头(您正在访问一个类,这是一个常量)。

此外,请确保在Gemfile中包含gem (Rails3要求在此文件中指定所有库)。

最后,您不能像您列出的那样使用Maruku。相反,请尝试:

代码语言:javascript
复制
class Post < ActiveRecord::Base

  ...

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

end

然后在您的视图中,您可以通过<%= @post.content_html %>进行访问。注意,为了提高性能,您可能应该使用回调(请参阅Active Record Callbacks)将其转换为HTML语言,但这应该可以让您正常运行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3697593

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档