Note that there are some explanatory texts on larger screens.

plurals
  1. POHaving trouble understanding some code (Ruby on Rails)
    primarykey
    data
    text
    <p>I posted a <a href="https://stackoverflow.com/questions/2739270/how-do-i-prevent-a-form-from-being-resubmitted-too-quickly-in-a-rails-application">question</a> awhile ago asking how I could limit the rate at which a form could be submitted from a rails application. I was helped by a very patient user and their solution works great. The code was for my comments controller, and now I find myself wanting to add this functionality to another controller, my Messages controller. I immediately tried reusing the working code from the comments controller but I couldn't get it to work. Instead of asking for the working code, could someone please help me understand my working comment controller code?</p> <pre><code>class CommentsController &lt; ApplicationController #... before_filter :post_check def record_post_time cookies[:last_post_at] = Time.now.to_i end def last_post_time Time.at((cookies[:last_post_at].to_i rescue 0)) end MIN_POST_TIME = 2.minutes def post_check return true if (Time.now - last_post_time) &gt; MIN_POST_TIME flash[:warning] = "You are trying to reply too fast." @message = Message.find(params[:message_id]) redirect_to(@message) return false end #... def create @message = Message.find(params[:message_id]) @comment = @message.comments.build(params[:comment]) if @comment.save record_post_time flash[:notice] = "Replied to \"#{@message.title}\"" redirect_to(@message) else render :action =&gt; "new" end end def update @message = Message.find(params[:message_id]) @comment = Comment.find(params[:id]) if @comment.update_attributes(params[:comment]) record_post_time redirect_to post_comment_url(@message, @comment) else render :action =&gt; "edit" end end #... end </code></pre> <p>My Messages controller is pretty much a standard rails generated controller with a few before filters and associated private methods for DRYing up the code and a redirect for non existent pages. </p> <p>I'll explain how much of the code I understand. When a comment is created, a cookie is created with a last_post_time value. If they try to post another comment, the cookie is checked if the last one was made in the last two minutes. If it was a flash warning is displayed and no comment is recorded. What I don't really understand is how the post_check method works and how I can adapt it for my simpler posts controller. I thought I could reuse all the code in the message controller with the exception of the line:</p> <pre><code>@message = Message.find(params[:message_id]) # (don't need the redirect code) </code></pre> <p>in the post_check method. I really want to understand this. Can someone explain why this doesn't work? I greatly appreciate you reading my lengthy question. </p> <p>Create and update actions of the Messages controller:</p> <pre><code>def create @message = Message.new(params[:message]) respond_to do |format| if @message.save record_post_time flash[:notice] = "Created \"#{@message.title}\"" format.html { redirect_to(messages_url) } else format.html { render :action =&gt; "new" } end end end def update respond_to do |format| if @post.update_attributes(params[:post]) record_post_time flash[:notice] = 'Post was successfully updated.' format.html { redirect_to(@post) } else format.html { render :action =&gt; "edit" } end end end </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload