在 Rails 3 中使用 Maruku 的问题

发布于 2024-09-19 01:50:16 字数 2603 浏览 5 评论 0原文

我是 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

我也在我的帖子控制器中尝试了类似的东西,我在这里找到了。然后在我的“显示”视图中调用@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

I am new to Rails but have used PHP extensively over the years. I am building a simple blog (I know) to get my skills up in the MVC/Rails world.

I have the basics working but have spent the weekend trying to get Maruku to work eg a post body saved from a text area with Markdown Extra markup to the db and then back again to the browser.

I used the following code in my Post model but I get an error when I try to load /posts - "undefined local variable or method `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

I also tried something similar in my Posts Controller that I found on here. Then called @post.content in my Show view but get an error:

body = maruku.new(post.body)
post.body = body.to_html

I am dead sure it's my noob brain being dead but any help or direction would be great as I have fought with this for two days now. BTW I am using maruku as I need Markdown Extra as my old blog posts are all formatted that way.

Thanks

UPDATED - 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

|煩躁 2024-09-26 01:50:16

你需要使用(注意大小写):

Maruku.new(...)

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

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

最后,您不能按照您列出的方式使用 Maruku。相反,尝试:

class Post < ActiveRecord::Base

  ...

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

end

然后在您看来,您可以通过<%= @post.content_html %>访问。请注意,您可能应该使用回调将其转换为 HTML(请参阅 Active Record 回调) 在某些时候提高性能,但这应该可以让您启动并运行。

You need to use (note the case):

Maruku.new(...)

Constants in ruby begin in an upper case letter and variables begin in a lower case letter (you are accessing a class, which is a constant).

Also, ensure that you include the gem in your Gemfile (Rails 3 requires all libraries be specified in this file).

Finally, you can not use Maruku as you listed. Instead, try:

class Post < ActiveRecord::Base

  ...

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

end

Then in your view, you can access through <%= @post.content_html %>. Note, you should probably do a conversion to HTML using a callback (see Active Record Callbacks) for improved performance at some point, but this should get you up and running.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文