0

首先,我在评论输入框中输入“你好”并提交。
它成功了,没有任何问题。
然后,如果我在 10 秒内再次尝试重做,我将其设计为返回“垃圾邮件警告”,它会按照我的意愿执行操作。但问题是,我在上一页输入的所有输入数据都不见了……所以我必须再次输入它们。
我怎样才能留下以前的输入数据?

def show
    ...
    @comment_input = @user.comment_threads.build
    ...
end


def add_comment
    @user = User.find_by_username(params[:id])
    @post = @user.comment_threads.last

    if @post
        last_time = @post.created_at
        if Time.now - last_time <= 10.second
            redirect_to :controller => 'users', :action => 'show', :id => @user.username
            flash[:notice] = "You cannot spam!"   
            return
        end
    end

    @user_who_commented = current_user
    @comment = Comment.build_from( @user, @user_who_commented.id, params[:comment][:body] )
    @comment.comment_icon = params[:comment][:comment_icon]
    @comment.save
    redirect_to :controller => 'users', :action => 'show', :id => @user.username
    flash[:notice] = "comment added!"
end

_form.html.erb

<%=form_for @comment_input, url: url_for( :controller => :users, :action => :add_comment ) do |f| %>

    <div class="field">
      <%= f.label :body %><br />
      <%= f.text_field :body %>
    </div>
    <div class="field">
    <%= f.file_field :comment_icon %>
    </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

更新:

users_controller.rb #show

def show    
    @user = User.find_by_username(params[:id])

        @comments = @user.comment_threads.order("updated_at DESC").page(params[:page]).per(5)
        @comment_input = @user.comment_threads.build

        respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @user }
        end 
    end

end

意见/用户/show.html.erb

.....
<table>
  <tr>
    <th>ID</th>
    <th>PIC</th>
    <th>Body</th>
    <th>Subject</th>
    <th>Posted by</th>
    <th>Delete</th>
  </tr>

<% @comments.each do |comment| %>
  <tr>
    <td><%= comment.id %></td>
    <td>
            <% if comment.comment_icon? %>
                <ul class="thumbnails">
                <%= image_tag(comment.comment_icon.url(:thumb),:height => 100, :width => 100, :style => 'border:3px double #545565;' ) %>
                </ul>
            <% end %>

    </td>
    <td><%= comment.body %></td>
    <td><%= comment.subject %></td>
    <td><%= comment.user.user_profile.nickname if comment.user.user_profile %></td>
    <td>
    <%= button_to 'destroy', delete_comment_user_path(@user,comment.id), confirm: 'Are you sure?', :disable_with => 'deleting...'  if current_user && current_user.id == comment.user_id %>
    </td>
    </tr>
<% end %>
</table>


<%=form_for @comment_input, url: url_for( :controller => :users, :action => :add_comment ) do |f| %>

    <div class="field">
      <%= f.label :body %><br />
      <%= f.text_field :body %>
    </div>
    <div class="field">
    <%= f.file_field :comment_icon %>
    </div>
  <div class="actions">
    <%= f.submit %>
  </div>

<% end %>
......
4

1 回答 1

1
    def add_comment
      @user = User.find_by_username(params[:id])
      @post = @user.comment_threads.last
      @user_who_commented = current_user
      @comments = @user.comment_threads.order("updated_at DESC").page(params[:page]).per(5)
      @comment = Comment.build_from( @user, @user_who_commented.id, params[:comment][:body] )
      @comment.comment_icon = params[:comment][:comment_icon]

      if @post && (Time.now - @post.created_at) <= 10.second
        flash[:notice] = "You cannot spam!"
        render :action => "show"
      elsif @comment.save
        flash[:notice] = "comment added!"
        redirect_to :controller => 'users', :action => 'show', :id => @user.username
      else # if model's errors
        render :action => "show"
      end
    end
于 2012-12-28T16:45:56.017 回答