调用markdown方法时,参数数量错误(1表示0)错误

时间:2015-06-04 03:19:22

标签: ruby-on-rails ruby markdown

所有

我刚创建了markdown_title和markdown_body方法。当我进入Post模型的show view页面时,我收到错误:参数数量错误。

我相信我的markdown_title方法(也适用于markdown_body)可能在post.rb文件的下面构造不正确。这是罪魁祸首吗?:

class Post < ActiveRecord::Base
  has_many :comments
  has_one :summary
  belongs_to :user 
  belongs_to :topic
  #has_one :summary
  default_scope {order('created_at DESC')}

  validates :title, length: {minimum: 5},  presence: true
  validates :body,  length: {minimum: 20}, presence: true
  validates :topic, presence: true
  validates :user, presence: true


 def markdown_title
(render_as_markdown).render(self.title).html_safe
end


def markdown_body
(render_as_markdown).render(self.body).html_safe
end

private

def render_as_markdown
renderer = Redcarpet::Render::HTML.new
extensions = {fenced_code_blocks: true}
redcarpet = Redcarpet::Markdown.new(renderer, extensions)
#return redcarpet
end


end

以下是我的show.html.erb文件的代码,其中在调用markdown_title方法时出现错误:

    <h1><%= @post.markdown_title @post.title %></h1>

    <div class="row">
    <div class="col-md-8">
    <p><%= @post.body %></p>
    </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>

    <% if @post.summary.present? %>

    <h1>Post Summary</h1>
    <p><%= @post.summary.body %></p>

    <% else %>

  <%= form_for [@topic, @post, @post.build_summary] do |f| %>
    <%= f.text_field :body %>
    <%= f.submit %>
  <% end %>

<% end %>

这是Post控制器:

class PostsController < ApplicationController
  #def index #for the index page
      #@posts = Post.all 
      #authorize @posts  
  #end

  def show
      @post = Post.find(params[:id])
      @topic = Topic.find(params[:topic_id])
  end

  def new
      @topic = Topic.find(params[:topic_id])
      @post = Post.new
        authorize @post #authorize() will check the policy on new post resources
       # if user is present it wll let it render if no user present itll give exception
  end

  def create
    #@post = Post.new(params.require(:post).permit(:title, :body))
    #require and permit make sure only certain keys are passed to Post.new
    @topic = Topic.find(params[:topic_id])
    #@post = current_user.posts.build(params.require(:post).permit(:title, :body))
    @post = current_user.posts.build(post_params)
    @post.topic = @topic
    authorize @post #authorize() will check if user is logged in if not itll give an exception

    if @post.save
      flash[:notice] = "Your new post was created and saved."
      redirect_to [@topic, @post] #takes you to the new post you created
    else
      flash[:error] = "There was an error saving the post. Please try again."
      render :new # it grabs the new.html.erb file and pastes it in the view
    end
  end


  def edit
      @topic = Topic.find(params[:topic_id])
      @post = Post.find(params[:id])
      authorize @post
  end

  def update
    @topic = Topic.find(params[:topic_id])
    @post = Post.find(params[:id])
    #@post_check = current_user.posts.build(post_params)
    authorize @post

    if @post.update_attributes(post_params)
      flash[:notice] = "Post was updated and captured your new update."
      redirect_to [@topic, @post]
    else
      flash[:error] = "There was an error saving the post. Please try again."
      render :new
    end
  end

  private

  def post_params
    params.require(:post).permit(:title, :body)
  end

end

以下是我对自己观点的错误: enter image description here

1 个答案:

答案 0 :(得分:1)

您使用参数调用markdown_title方法,在本例中为@post.title

<h1><%= @post.markdown_title @post.title %></h1>

Post类的定义中,markdown_title方法不接受任何参数。

 def markdown_title
   (render_as_markdown).render(self.title).html_safe
 end

这就是您看到Wrong number of arguments (1 for 0)错误的原因。

由于您已在self.title方法中引用markdown_title,因此没有理由将@post.title传递给它。只需从您呼叫@post.title的地方删除markdown_title,就可以开始了。