Ruby on rails 红地毯降价转换方法,NoMethodError
在这里做作业。刚刚介绍了红地毯和降价转换方法 这是放置在我的助手中的:Ruby on rails 红地毯降价转换方法,NoMethodError,ruby-on-rails,ruby,ruby-on-rails-3,markdown,redcarpet,Ruby On Rails,Ruby,Ruby On Rails 3,Markdown,Redcarpet,在这里做作业。刚刚介绍了红地毯和降价转换方法 这是放置在我的助手中的: def markdown_to_html(markdown) renderer = Redcarpet::Render::HTML.new extensions = { fenced_code_blocks: true } redcarpet = Redcarpet::Markdown.new(renderer, extensions) (redcarpet.render markdown).html_saf
def markdown_to_html(markdown)
renderer = Redcarpet::Render::HTML.new
extensions = { fenced_code_blocks: true }
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
(redcarpet.render markdown).html_safe
end
在我的观点中,我可以这样调用该方法:
views/posts/show.html.erb:
<h1><%= markdown_to_html @post.title %></h1>
<div class="row">
<div class="col-md-8">
<p><%= markdown_to_html @post.body %>
</div>
<div class="col-md-4">
<% if policy(@post).edit? %>
<%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
<% end %>
</div>
</div>
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :topic
# Sort by most recent posts first
default_scope { order('created_at DESC') }
# Post must have at least 5 characters in the title
validates :title, length: { minimum: 5 }, presence: true
# Post must have at least 20 characters in the body
validates :body, length: { minimum: 20 }, presence: true
# Post must have an associated topic and user
validates :topic, presence: true
validates :user, presence: true
def render_as_markdown(markdown)
renderer = Redcarpet::Render::HTML.new
extensions = { fenced_code_blocks: true }
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
(redcarpet.render markdown).html_safe
end
private
def markdown_title(markdown)
render_as_markdown(markdown).title
end
def markdown_body(markdown)
render_as_markdown(markdown).body
end
end
现在我要做以下几件事:
目标是按如下方式渲染降价:
将帖子#降价#标题和帖子#降价#正文添加到帖子:
创建一个私有的Post#render _as _markdown方法来标记标题
而markdown_身体可以呼叫。这将保留降价标题和
把你的身体弄干
从application_helper.rb中删除markdown_to_html方法
更新您的视图,以使用帖子#降价#标题和
后减价法
到目前为止,我已尝试这样做:
models/post.rb:
<h1><%= markdown_to_html @post.title %></h1>
<div class="row">
<div class="col-md-8">
<p><%= markdown_to_html @post.body %>
</div>
<div class="col-md-4">
<% if policy(@post).edit? %>
<%= link_to "Edit", edit_topic_post_path(@topic, @post), class: 'btn btn-success' %>
<% end %>
</div>
</div>
class Post < ActiveRecord::Base
has_many :comments
belongs_to :user
belongs_to :topic
# Sort by most recent posts first
default_scope { order('created_at DESC') }
# Post must have at least 5 characters in the title
validates :title, length: { minimum: 5 }, presence: true
# Post must have at least 20 characters in the body
validates :body, length: { minimum: 20 }, presence: true
# Post must have an associated topic and user
validates :topic, presence: true
validates :user, presence: true
def render_as_markdown(markdown)
renderer = Redcarpet::Render::HTML.new
extensions = { fenced_code_blocks: true }
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
(redcarpet.render markdown).html_safe
end
private
def markdown_title(markdown)
render_as_markdown(markdown).title
end
def markdown_body(markdown)
render_as_markdown(markdown).body
end
end
class Post
如果我们回到我的视图/posts/show.html.erb:
<h1><%= @post.title.markdown_title %></h1>
将呈现:
职位中的命名错误#显示
用于“斩波器杂志”的未定义方法'markdown_title':字符串
提取的源(第1行附近):
我做错了什么,如何纠正这个问题
谢谢。这里有几件事。首先,您在类中已将
markdown\u title
设置为私有方法,因此在视图中无法访问它。您需要删除markdown_title
和markdown_body
方法上方的单词private
,以便视图可以使用它们。此外,由于要求将render\u设置为\u markdown
private,因此需要将其移动到private
关键字下方。长话短说,您的方法在类中的结构应如下所示:
def markdown_title(markdown)
...
end
def markdown_body(markdown)
...
end
private
def render_as_markdown(markdown)
...
end
第二,如果你看一下markdown\u title
和markdown\u body
应该如何被调用(如下所示),它们不包含任何参数
<%= post.markdown_title %>
<%= post.markdown_body %>
然后,在您看来,您可以根据要求使用markdown\u title
和markdown\u body
:
<h1><%= @post.markdown_title %></h1>
<div class="row">
<div class="col-md-8">
<p><%= @post.markdown_body %>
</div>
...
</div>
...
非常感谢您解释我做错了什么,以及应该怎么做。这很有效。干杯杰出的很乐意帮忙。
<h1><%= @post.markdown_title %></h1>
<div class="row">
<div class="col-md-8">
<p><%= @post.markdown_body %>
</div>
...
</div>