更新对象时将对象ID传递到Rails Mailer视图

时间:2018-07-10 08:14:22

标签: ruby-on-rails

我正在尝试在企业更新帖子时向企业的关注者发送电子邮件通知。这是我的设置:

def update
  if @post.update(post_params)
    respond_to do |format|
    format.html { redirect_to posts_index_path, notice: "Updated post"
    format.json { render :'shows/show', status: :ok, location: @post }
    update_page_email_followers 
  else
    format.html { render :edit }
    format.json { render json: @post.errors, status: :unprocessable_entity }
    end
  end
end

def update_page_email_followers
  @followers = Follow.where(business_id: current_business.id)
  @followers.each do |follower|
    ModelMailer.delay.update_post(@user, follower, @post)
  end
end 

这是邮件方法:

def update_post(post follower, business_id)
  @business = Business.find(follower.business_id)
  @post = post
  @user = User.find(follower.user_id)
  mail(:to => @user.email, :subject => "#{@business.account.business_name} updated a post")
end

这就是邮件视图模板update_post.html.erb中的内容:

<%= @business.account.business_name %> has just updated a post! Please click on <%= link_to "Updated post", "https://www.example.com/sample-#{@post.id}" %> 

邮件通知已正确发送给所有关注者,我只是无法从以下位置在电子邮件视图中获得帖子的ID:<%= link_to "Updated post", "https://www.example.com/sample-#{@post.id}" %> ...显示为nil

有什么主意我可能会在这里错过吗?

1 个答案:

答案 0 :(得分:1)

def update_page_email_followers
  @followers = Follow.where(business_id: current_business.id)
  @followers.each do |follower|
    ModelMailer.delay.update_post(@user, follower, @post)
  end
end 

更新帖子方法

def update_post(user, follower, post)
  @business = Business.find(follower.business_id)
  @post = post
  @user = User.find(follower.user_id)
  mail(:to => @user.email, :subject => "#{@business.account.business_name} updated a post")
end