Wrong number of arguments (1 for 0) 调用降价方法时出错

Wrong number of arguments (1 for 0) error when calling markdown method

全部,

我刚刚创建了 markdown_title 和 markdown_body 方法。当我转到 Post 模型的显示视图页面时,出现错误:参数数量错误。

我认为我的 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

这是我在视图中遇到的错误:

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

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

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

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

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

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