0

这是我添加到 Rails 3 代码中的模型中的类方法

class Micropost < ActiveRecord::Base

def self.without_review
    where(review: false)
  end

仅供参考,这是显示“review”的 schema.db

 create_table "microposts", :force => true do |t|
    t.text     "content"
    t.boolean  "review",          :default => false
  end

所有帖子都默认为review=false,但如果用户在创建之前选中了一个框,那么review=true。

这是闪存消息所在的控制器

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Posted"
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end

如果review=false,我想要与现在相同的行为,但是如果review=true,我想闪烁一条消息,上面写着“Post is under review”而不是“Posted”

4

3 回答 3

0

只需进行这些更改create

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      if @micropost.review 
        # If review is true. The object @micropost is already built with necessary 
        # parameters sent by the form, say whether review is true.
        flash[:notice] = "Post is under review"
      else
        flash[:success] = "Posted"
        redirect_to root_path
      end
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end
于 2013-04-06T18:35:25.523 回答
0

另一种方法是:

  def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = "Posted"
      flash[:success] = "Post is under review" if @micropost.review 
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end
于 2013-04-06T18:39:29.713 回答
0
def create
    @micropost = current_user.microposts.build(params[:micropost])
    if @micropost.save
      flash[:success] = @micropost.review ? "Posted" : "Post is under review"
      redirect_to root_path
    else
      @feed_items = []
      render 'static_pages/home'
    end
  end
于 2013-04-06T18:59:05.663 回答