views / notifier中的html没有看到我的模型属性。错误:NoMethodError

时间:2015-12-03 19:35:30

标签: ruby-on-rails ruby ruby-on-rails-4

问候! 我是Rails和Rails 4的新手,我使用Beginning Rails 4,3rd Edition学习它。而且,当我的邮件程序尝试在html中访问它时,我遇到了获取属性的问题。这是我的问题:

文章中的

NoMethodError#notify_friend

显示/home/alex/Documents/curso_rails4/blog/app/views/notifier/email_friend.html.erb第14行:

未定义的方法`标题'对于#<阵列:0x007f4521586278> (我不得不把空间放在< A出现在这里。)

提取的来源(第14行):

           </p>
            <p>
                Come check all the information about <strong><%= @article.title %></strong> at
                <%= article_url(@article, :host => "localhost:3000") %>
            </p>
        </body>

申请追踪:
  app / views / notifier / email_friend.text.erb:3:in`_app_views_notifier_email_friend_text_erb ___ 902372559744089873_69967442421020&#39;

app / mailers / notifier.rb:17:在`email_friend&#39;

app / controllers / articles_controller.rb:72:在`notify_friend&#39;

文章控制器:

class ArticlesController < ApplicationController
  before_action :set_article, only: [:show, :edit, :update, :destroy]
  # Applying filter here
  before_action :authenticate, except: [:index, :show]

  # GET /articles
  # GET /articles.json
  def index
    @articles = Article.all
  end

  # GET /articles/1
  # GET /articles/1.json
  def show
  end

  # GET /articles/new
  def new
    @article = Article.new
  end

  # GET /articles/1/edit
  def edit
    @article = current_user.articles.find(params[:id])
  end

  # POST /articles
  # POST /articles.json
  def create
    # @article = Article.new(article_params)
    @article = current_user.articles.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: 'Article was successfully created.' }
        format.json { render :show, status: :created, location: @article }
      else
        format.html { render :new }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /articles/1
  # PATCH/PUT /articles/1.json
  def update
    @article = current_user.articles.find(params[:id])
    respond_to do |format|
      if @article.update(article_params)
        format.html { redirect_to @article, notice: 'Article was successfully updated.' }
        format.json { render :show, status: :ok, location: @article }
      else
        format.html { render :edit }
        format.json { render json: @article.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /articles/1
  # DELETE /articles/1.json
  def destroy
    @article = current_user.articles.find(params[:id])
    @article.destroy
    respond_to do |format|
      format.html { redirect_to articles_url, notice: 'Article was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def notify_friend
    @article = Article.find([params[:id]])
    Notifier.email_friend(@article, params[:name], params[:email]).deliver
    redirect_to @article, :notice => "Successfully sent a message to your friend"
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_article
      @article = Article.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def article_params
      params.require(:article).permit(:title, :location, :excerpt, :body, :published_at, :category_ids => [])
    end
end

邮件通知程序

class Notifier < ApplicationMailer
  def email_friend(article, sender_name, receiver_email)
    @article = article
    @sender_name = sender_name
    mail :to => receiver_email, :subject => "Interesting Article"
  end
end

在views / notifier中的Email_friend.html.erb

<html>
    <body>
        <p>
            One of your friends, <%= @sender_name %>, thinks you like an article we have written.
        </p>
        <p>
            Come check all the information about <strong><%= @article.title %></strong> at
            <%= article_url(@article, :host => "localhost:3000") %>
        </p>
    </body>
</html>

1 个答案:

答案 0 :(得分:1)

你的问题是你在这里使用一个数组,这表明你想要一个结果数组: @article = Article.find([params[:id]])

如果你改为@article = Article.find(params[:id]),你应该没问题!