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是否在控制器中保存草稿更新?_Ruby On Rails_Ruby_Ruby On Rails 3 - Fatal编程技术网

Ruby on rails Rails是否在控制器中保存草稿更新?

Ruby on rails Rails是否在控制器中保存草稿更新?,ruby-on-rails,ruby,ruby-on-rails-3,Ruby On Rails,Ruby,Ruby On Rails 3,我有一个名为articles的模型,它有一个字符串字段,允许用户将文章设置为草稿。当选择了草稿并且用户更新了文章时,我希望它返回到文章编辑页面,就像用户选择了发布选项一样,然后我希望用户被重定向到文章索引页面 问题是,如果选择了草稿选项,我无法更新文章并将其重定向回帖子。我是不是走错了路 迁移文件 def change add_column :articles, :status, :string, default: 'Draft' end 文章.rb scope :submitte

我有一个名为articles的模型,它有一个字符串字段,允许用户将文章设置为草稿。当选择了草稿并且用户更新了文章时,我希望它返回到文章编辑页面,就像用户选择了发布选项一样,然后我希望用户被重定向到文章索引页面

问题是,如果选择了草稿选项,我无法更新文章并将其重定向回帖子。我是不是走错了路

迁移文件

def change
    add_column :articles, :status, :string, default: 'Draft'
  end
文章.rb

scope :submitted, lambda { where('status = ?', 2) }
scope :draft, lambda{ where('status = ?', 1) } 

def is_draft?
  self.draft
end
物品管理员

  def update
      case @article.status
        when 1 
          @article.status = 'Draft'
        else 2 
          @article.status = 'Published'
      end

      if @article.status == 1 
        @article = article.find(params[:id])
        flash[:notice] = "Successfully Updated" if @article.update_attributes(params[:article])
        respond_with(@article, :location => edit_article_path)
      else
        @article = article.find(params[:id])
        flash[:notice] = "Successfully Updated" if @article.update_attributes(params[:article])
        respond_with(@article, :location => articles_path)
      end
  end

如果您确实希望使用1/2的值

型号:

STATUS_VALUES = {1 => "Draft", 2 => "Published"}

scope :submitted, lambda { where('status = ?', STATUS_VALUES[2]) }
scope :draft, lambda{ where('status = ?', STATUS_VALUES[1]) } 

attr_accessible :_status

after_initialize do
  self.draft! if self.new_record?  # be draft by default
end

def draft!
  self.status = STATUS_VALUES[1]
end

def published!
  self.status = STATUS_VALUES[2]
end

def _status
  STATUS_VALUES.invert(status)
end

def _status=(value)
  case value
  when 1, "1" then self.draft!
  when 2, "2" then self.published!
  else self.draft!
  end
end

def draft?
  self.status == STATUS_VALUES[1]
end

def published?
  self.status == STATUS_VALUES[2]
end
控制器:

def update
  @article = article.find(params[:id])
  if @article.update_attributes(params[:article])
    flash[:notice] = "Successfully Updated" 
    if @article.draft?
      respond_with(@article, :location => edit_article_path)
    else
      respond_with(@article, :location => articles_path)
    end
  else
    render :action => :edit
  end
end
视图:


<%= f.check_box(:_status, "Published", 2, 1) %>