总而言之,我有一个blog_posts
控制器。控制器主要blog_posts
用作普通 CRUD。但是,在模型中,我记录state
了blog_posts
. 所以用户可以发布一个条目,但它不会处于Active 状态,这意味着没有人可以查看它。一旦他们将状态更改为Active,就可以查看它,因此建议blog_post
保持静态并且用户不要更新它,但是,我们仍然允许更新,并带有警告。
所以,我的任务是创建一个warning
防火墙页面。当用户单击Activeedit
时,应出现警告页面,然后他们必须单击继续或返回。 blog_post
我的代码如下所示:
def edit
@blog = Blog.find(params[:blog_id])
@blog_post = @blog.blog_posts.find(params[:id])
if @blog_post.state == "Active" && @blog_post.editing_after_active.nil?
redirect_to warning_path(@blog, @blog_post)
end
end
def warning
@blog = Blog.find(params[:blog_id])
@blog_post = @blog.blog_posts.find(params[:id])
if params[:no]
@blog_post.update_attributes(editing_after_active: nil)
redirect_to blog_blog_posts_path(@blog)
elsif params[:yes]
@blog_post.update_attributes(editing_after_active: "yes")
redirect_to edit_blog_blog_post_path(@blog, @blog_post)
end
end
这就是我相信它的工作方式。
- 用户导航到活动帖子的 blog_post 编辑页面。
- 编辑控制器看到帖子处于活动状态,并且用户没有接受警告,并重定向到警告页面。(Is Active 和 editing_after_active 都评估为 true)
- 在警告页面上,有两个选项(是/否)。
- 用户单击是继续。editor_after_active 属性应该设置为“yes”,这意味着他们已经接受了警告。
- 他们被重定向到编辑。
- 再次评估标准,但这一次,它评估为假。
- 呈现编辑页面。
我在这里想念什么?堆栈级别太深应该用于无限循环。这个循环是无限的吗?
更新:
class BlogPost < ActiveRecord::Base
include BlogModule
belongs_to :blog
has_one :visit, :as => :visitable
has_many :assets, :as => :assetable, :dependent => :destroy
has_many :bookmarks, :dependent => :destroy
has_many :bookmark_users, :through => :bookmarks, :dependent => :destroy
has_many :taggings, :as => :taggable, :dependent => :destroy
has_many :tags, :through => :taggings
# more has_many
attr_accessible :tag_ids, :asset_ids # more attr_accessible
attr_accessor :editing_after_active
def editing_after_active
editing_after_active
end
end
更新:
Connecting to database specified by database.yml
Started POST "/blogs/resort-casino-chicago/blog_posts/39/warning" for 127.0.0.1 at 2014-02-27 14:41:57 -0500
Processing by blogpostsController#warning as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"6UaDU1Sn3yiKlGl6OJCUapmU4KCSilLL8Lgo+jsyJdI=", "yes"=>"Yes, I am authorized to continue.", "blog_id"=>"resort-casino-chicago", "blog_post_id"=>"39"}
[1m [36mblog Load (0.8ms) [0m [1mSELECT `blogs`.* FROM `blogs` WHERE `blogs`.`slug` = 'resort-casino-chicago' LIMIT 1 [0m
[1m [35mblogpost Load (0.2ms) [0m SELECT `blog_posts`.* FROM `blog_posts` WHERE `blog_posts`.`blog_id` = 1680 AND `blog_posts`.`id` = 39 LIMIT 1
[1m [36m (0.2ms) [0m [1mBEGIN [0m
[1m [35m (0.1ms) [0m COMMIT
Redirected to http://localhost:3000/blogs/resort-casino-chicago/blog_posts/39/edit
Completed 302 Found in 647ms (ActiveRecord: 8.4ms)
Started GET "/blogs/resort-casino-chicago/blog_posts/39/edit" for 127.0.0.1 at 2014-02-27 14:41:58 -0500
Processing by blogpostsController#edit as HTML
Parameters: {"has_many"=>:posts, "blog_id"=>"resort-casino-chicago", "id"=>"39"}
[1m [36mblog Load (0.9ms) [0m [1mSELECT `blogs`.* FROM `blogs` WHERE `blogs`.`slug` = 'resort-casino-chicago' LIMIT 1 [0m
[1m [35mblogpost Load (0.3ms) [0m SELECT `blog_posts`.* FROM `blog_posts` WHERE `blog_posts`.`blog_id` = 1680 AND `blog_posts`.`id` = 39 LIMIT 1
Completed 500 Internal Server Error in 5ms
SystemStackError - stack level too deep:
actionpack (3.2.11) lib/action_diblogtch/middleware/reloader.rb:70:in `'
Started POST "/__better_errors/70165434709380/variables" for 127.0.0.1 at 2014-02-27 14:41:58 -0500
更新:
好的,在警告页面上,我尝试渲染 <%= @blog_post.editing_after_active %> 的当前值,但仅仅因为尝试渲染该值而导致堆栈级别太深。因此,错误最有可能与设置虚拟属性有关。
我像这样手动设置editing_after_active的值: def editing_after_active "yes" end
有了这个警告表单提交就好了。所以,需要弄清楚为什么原来的设置是递归的。