Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在Rails 3中应用该类方法后,如何添加备用flash消息?_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails 在Rails 3中应用该类方法后,如何添加备用flash消息?

Ruby on rails 在Rails 3中应用该类方法后,如何添加备用flash消息?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,这是我添加到Rails 3代码中的模型中的类方法 class Micropost < ActiveRecord::Base def self.without_review where(review: false) end 所有帖子都默认为review=false,但是如果用户在创建之前选中了一个框,那么review=true 这是显示闪存消息的控制器 def create @micropost = current_user.microposts.build(pa

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

class Micropost < ActiveRecord::Base

def self.without_review
    where(review: 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 review”,而不是“Posted”

只需在
上进行这些更改即可创建

  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
另一种方式是:

  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

使用上述代码,我在该控制器中遇到语法错误<代码>意外关键字\u else,应为关键字\u end意外关键字\u end,应为$end。应该有多少个
end
?我尝试了各种不同的数量,但无法解决错误忘记为if块添加
end
,这很容易发现。修订版。你知道为什么我的
模板丢失了吗?当我在写一篇文章时,选中了复选框,使文章评论为真时,缺少了模板microspost/create
。@AmyBrown,我想你需要检查一下你的routes.rb。您可能已经通过一些自动生成的代码在
GET
中定义了
create
。尝试设置
resources:microposts
  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
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