title: AbstractController::DoubleRenderError at /groups/24 Render and/or redirect were called multiple times in this action
body: <p>I built the system that users can see group#show pages only when users login. It worked. But I changed my mind. Now I would like visitors also to see the group#show pages even if they don't login.And I removed some codes when they don't login because session related code like </p>
  
  <pre><code> #@member = Member.find(session[:user_id])
              #@isGr = GroupInMember.where(:member_id =&gt; session[:user_id], :group_id =&gt; 
                 #params[:id].to_i).count &gt; 0  
  </code></pre>
  
  <p>would cause some errors.</p>
  
  <p>Although I think I could avoid the errors that come from session[:user_id], I encountered the error below. How can I solve this problem?</p>
  
  <pre><code>AbstractController::DoubleRenderError at /groups/24
  Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
  </code></pre>
  
  <p>Here is my code;</p>
  
  <p>☆groups_controller</p>
  
  <pre><code>def show
  
      @group = Group.find(params[:id])
      @group_message = Group.find(params[:id]).group_messages.build
      if checklogin? 
          @group_messages = Group.find(params[:id]).group_messages.order("created_at desc")
          @me = me?
          @member = Member.find(session[:user_id])
          @isGr = GroupInMember.where(:member_id =&gt; session[:user_id], :group_id =&gt; 
              params[:id].to_i).count &gt; 0
         @gms = Group.find(params[:id]).group_messages.order("created_at desc").scoped
            if params[:page].present?
             @gms = @gms.where("page = ?" ,  params[:page] )
            end
            if params[:content].present?
             @gms = @gms.where("content like ?" , "%" + params[:content] + "%")
            end
             respond_to do |format|
              format.html # index.html.erb
              format.json { render json: @group_messages }
            end       
      else
          @group_messages = Group.find(params[:id]).group_messages.order("created_at desc")
          #@me = me?
          #@member = Member.find(session[:user_id])
          #@isGr = GroupInMember.where(:member_id =&gt; session[:user_id], :group_id =&gt; 
             #params[:id].to_i).count &gt; 0        
         @gms = Group.find(params[:id]).group_messages.order("created_at desc").scoped
            if params[:page].present?
             @gms = @gms.where("page = ?" ,  params[:page] )
            end
            if params[:content].present?
             @gms = @gms.where("content like ?" , "%" + params[:content] + "%")
            end
            redirect_to '/groups/' + params[:id].to_s   and return
      end    
    end
  </code></pre>
  
  <p>☆show.html.erb(groups_controller)</p>
  
  <pre><code>&lt;div class="container-fluid"&gt;
    &lt;div class="row-fluid"&gt;
      &lt;div class="span4"&gt;
        &lt;div id="individual"&gt; 
          &lt;h4&gt;※「&lt;%= @group.name %&gt;」のページ&lt;/h4&gt;
          &lt;%= link_to image_tag(@group.imageurl, :width=&gt;"100" ,:height=&gt;"150"),@group.detailurl , :target =&gt; '_blank' %&gt;
          &lt;table class="table"&gt;
            &lt;thead&gt;
            &lt;tr&gt;
              &lt;th&gt;#&lt;/th&gt;
              &lt;th&gt;プロフィール&lt;/th&gt;
            &lt;/tr&gt;
            &lt;/thead&gt;
            &lt;tbody&gt;
                &lt;tr&gt;
                  &lt;th&gt;著者&lt;/th&gt;
                  &lt;td&gt;&lt;%= @group.author %&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;th&gt;出版年&lt;/th&gt;
                  &lt;td&gt;&lt;%= @group.published %&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;th&gt;出版社&lt;/th&gt;
                  &lt;td&gt;&lt;%= @group.publish %&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;th&gt;ページ数&lt;/th&gt;
                  &lt;td&gt;&lt;%= @group.page %&gt;&lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr&gt;
                  &lt;th&gt;ISBN&lt;/th&gt;
                  &lt;td&gt;&lt;%=  @group.isbn %&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/tbody&gt;
          &lt;/table&gt;
  
        &lt;/div&gt;&lt;!--individual--&gt;
          &lt;%# if @group.admin %&gt;
            &lt;%#= link_to 'Profile Edit', edit_group_path(@group),class: "btn btn-midium btn-primary" %&gt;
          &lt;%# end %&gt;  
            &lt;hr&gt;
     &lt;% if session[:user_id]%&gt;       
        &lt;div class="group-side"&gt;
            &lt;% if @isGr %&gt;
                ※登録済みです。
            &lt;% else %&gt;
                ※登録していません。
           &lt;% end %&gt;
        &lt;/div&gt;
          &lt;p class="group-side"&gt;
          &lt;%= link_to '本棚登録/解除', {:controller =&gt; 'groups', :action =&gt; 'join', :id =&gt;
          @group.id }, class: "btn btn-midium btn-primary"%&gt;&lt;/p&gt;
        &lt;/p&gt;
        &lt;p&gt;&lt;/p&gt;
        &lt;div class="group-side2"&gt;&lt;b&gt;この本を登録した人一覧:&lt;/b&gt;&lt;/div&gt;
          &lt;% @group.group_in_members.each do |m| %&gt;
           &lt;% @member = Member.find(m.member_id) %&gt;
             &lt;% if @member.provider %&gt;
              &lt;a href="/members/&lt;%= m.member_id %&gt;" rel="tooltip" title="&lt;%= raw(@member.name) %&gt;"&gt;&lt;%= image_tag @member.image, :width =&gt;'30px', :height =&gt;'30px' %&gt;&lt;/a&gt;
            &lt;% elsif @member.avatar_file_name %&gt;
              &lt;a href="/members/&lt;%= m.member_id %&gt;" rel="tooltip" title="&lt;%= raw(@member.name) %&gt;"&gt;&lt;%= image_tag @member.avatar.url(:thumb), :width =&gt;'30px', :height =&gt; '30px' %&gt;&lt;/a&gt;
            &lt;% else %&gt;
              &lt;a href="/members/&lt;%= m.member_id %&gt;" rel="tooltip" title="&lt;%= raw(@member.name) %&gt;"&gt;&lt;%= image_tag 'love.png', :width =&gt;'30px', :height =&gt; '30px' %&gt;&lt;/a&gt; 
            &lt;% end %&gt;
          &lt;%# if GroupInMember.where( :member_id =&gt; session[:user_id], :group_id =&gt; @group.id ).length &gt; 0 %&gt;
          &lt;%# end %&gt;  
        &lt;% end %&gt;             
        &lt;/div&gt;
    &lt;% end %&gt;
      &lt;div class="span8"&gt;
        &lt;p&gt;
          &lt;b&gt;※ページと行数は半角数字。ページと投稿内容の入力は必須です。&lt;/b&gt;
        &lt;/p&gt;
        &lt;div class="post_on_group"&gt;
          &lt;% if GroupInMember.where(:member_id =&gt;session[:user_id], :group_id=&gt;@group.id).length &gt; 0 %&gt;
            &lt;%= form_for([@group, @group_message]) do |f| %&gt;
                  &lt;% if @group_message.errors.any? %&gt;
                &lt;div id="error_explanation"&gt;
                  &lt;h2&gt;&lt;%= pluralize(@group_message.errors.count, "error") %&gt; prohibited this group_message from being saved:&lt;/h2&gt;
                  &lt;ul&gt;
                  &lt;% @group_message.errors.full_messages.each do |msg| %&gt;
                    &lt;li&gt;&lt;%= msg %&gt;&lt;/li&gt;
                  &lt;% end %&gt;
                  &lt;/ul&gt;
                &lt;/div&gt;
              &lt;% end %&gt;
              &lt;div class="field"&gt;
                &lt;%#= f.label :member_id %&gt;
                &lt;%= f.hidden_field :member_id, :value =&gt; session[:user_id] %&gt;
              &lt;/div&gt;
               &lt;div class="field"&gt;
                &lt;%#= f.label :group_id %&gt;
                &lt;%= f.hidden_field :group_id %&gt;
              &lt;/div&gt;
              &lt;div class="form_index2"&gt;
                &lt;%= f.label :"何ページ？" %&gt;
                &lt;%= f.text_field :page, :class=&gt; "span3" %&gt;
              &lt;/div&gt;
              &lt;div class="form_index4"&gt;
                &lt;%= f.label :"何行目あたり？" %&gt;
                &lt;%= f.text_field :line, :class=&gt; "span1" %&gt;
              &lt;/div&gt;  
              &lt;div class="form_index4"&gt;
                &lt;%= f.label :"投稿内容" %&gt;
                &lt;%= f.text_area :content ,:class =&gt; "span12",:size =&gt; "20x5" %&gt;
              &lt;/div&gt;
              &lt;div class="actions"&gt;
                &lt;%= f.submit "投稿"%&gt;
              &lt;/div&gt;
            &lt;% end %&gt;
           &lt;% end %&gt; 
  
        &lt;/div&gt; &lt;!--post_on_group--&gt;
        &lt;div&gt;
          ※この本に関する投稿をページ数や投稿内容から検索できます。
         &lt;/div&gt; 
           &lt;div class= "form_index"&gt;
                &lt;%= form_tag({:action=&gt;"show"}, {:method=&gt;"get"}) do %&gt;
                 &lt;div class="from_field_index5"&gt;
                  &lt;%= text_field_tag 'page' ,'', :class=&gt; "span3" %&gt;
                 &lt;%= submit_tag 'ページ'   %&gt;
                &lt;% end %&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;div class= "form"&gt;
                &lt;%= form_tag({:action=&gt;"show"}, {:method=&gt;"get"}) do %&gt;
                 &lt;div class="from_field_index"&gt;
                  &lt;%= text_field_tag 'content' %&gt;
                 &lt;%= submit_tag '投稿内容' %&gt;
                &lt;% end %&gt;
                &lt;/div&gt;
              &lt;/div&gt;
  
  
        &lt;hr&gt;
        &lt;br/&gt;
        &lt;% if @gms %&gt;
           &lt;% @gms.each do |gm| %&gt;
                   &lt;div class="message_area"&gt;
                        &lt;div class="each_message"&gt;
                          &lt;%= image_tag gm.group.imageurl,:width =&gt; '20', :height =&gt; '25' %&gt;
  
  
                            &lt;%= 'Page:' + gm.page.to_s + '＆' %&gt;&lt;%= 'Line:' + gm.line.to_s %&gt;
                          &lt;a href="/members/&lt;%= gm.member.id%&gt;"&gt;&lt;%= gm.member.name %&gt;&lt;/a&gt;
                          (&lt;%= gm.created_at.strftime'%Y-%m-%d %H:%M' %&gt;)
                         &lt;div class="group_message"&gt;  
                          &lt;p class="message_content"&gt;&lt;a href="/group_messages/&lt;%= gm.id%&gt;" &gt; &lt;%= truncate(gm.content, { :length =&gt; 300}) %&gt;&lt;/a&gt;&lt;/p&gt;
                         &lt;/div&gt;&lt;!--group_message--&gt;
                         &lt;br/&gt;
                          &lt;% if gm.group_message_comments.present? %&gt;
                             &lt;% gm.group_message_comments.order("created_at asc").each do |gmsc|%&gt;
                              &lt;div class="group_message_comment"&gt;
                                 &lt;p&gt;&lt;a href="/members/&lt;%= gmsc.member ? gmsc.member.id : "unknown" %&gt;"&gt;&lt;%= gmsc.member ? gmsc.member.name : "unknown" %&gt;&lt;/a&gt;  (&lt;%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %&gt;)&lt;/p&gt;
                                 &lt;%= truncate(gmsc.content, { :length =&gt; 300}) %&gt; 
                              &lt;/div&gt; &lt;!--group_message_comment--&gt;
                              &lt;br/&gt;
                             &lt;% end %&gt;&lt;!-- each do -- &gt;
                          &lt;% else %&gt; 
                          &lt;% end %&gt; &lt;!--if --&gt;
                        &lt;/div&gt;&lt;!--each_message--&gt;
                        &lt;br&gt;
                   &lt;/div&gt; &lt;!--message_area--&gt; 
                 &lt;% end %&gt;   
        &lt;% else %&gt;
           &lt;% @group_messages.each do |gm| %&gt; 
                      &lt;div class="message_area"&gt;
                          &lt;div class="each_message"&gt;
                            &lt;%= image_tag gm.group.imageurl,:width =&gt; '20', :height =&gt; '25' %&gt;
                              &lt;%= 'Page:' + gm.page.to_s + '＆' %&gt;&lt;%= 'Line:' + gm.line.to_s %&gt;
                            &lt;a href="/members/&lt;%= gm.member.id%&gt;"&gt;&lt;%= gm.member.name %&gt;&lt;/a&gt;
                            (&lt;%= gm.created_at.strftime'%Y-%m-%d %H:%M' %&gt;)
                           &lt;div class="group_message"&gt;  
                            &lt;p class="message_content"&gt;&lt;a href="/group_messages/&lt;%= gm.id%&gt;" &gt; &lt;%= truncate(gm.content, { :length =&gt; 300}) %&gt;&lt;/a&gt;&lt;/p&gt;
                           &lt;/div&gt;&lt;!--group_message--&gt;
                           &lt;br/&gt;
                            &lt;% if gm.group_message_comments.present? %&gt;
                               &lt;% gm.group_message_comments.each do |gmsc|%&gt;
                                &lt;div class="group_message_comment"&gt;
                                   &lt;p&gt;&lt;a href="/members/&lt;%= gmsc.member ? gmsc.member.id : "unknown" %&gt;"&gt;&lt;%= gmsc.member ? gmsc.member.name : "unknown" %&gt;&lt;/a&gt;  (&lt;%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %&gt;)&lt;/p&gt;
                                   &lt;%= truncate(gmsc.content, {:length =&gt;300}) %&gt; 
                                &lt;/div&gt; &lt;!--group_message_comment--&gt;
                                &lt;br/&gt;
                               &lt;% end %&gt;&lt;!-- each do -- &gt;
                            &lt;% else %&gt; 
                            &lt;% end %&gt; &lt;!--if --&gt;
                          &lt;/div&gt;&lt;!--each_message--&gt;
                          &lt;br&gt;
                     &lt;/div&gt; &lt;!--message_area--&gt;
                  &lt;% end %&gt;  &lt;!--each do --&gt;
  
        &lt;% end %&gt;
      &lt;/div&gt;&lt;!--span8--&gt;
  </code></pre>
  
  <p>☆login.html.erb(members_controller)</p>
  
  <pre><code> &lt;div class="span4"&gt;
                    &lt;h4 class="title"&gt;Users' reading texts&lt;/h4&gt;
  
                          &lt;% @books.each  do |book| %&gt;   
                             &lt;a href="/groups/&lt;%= book.id %&gt;" rel="tooltip" title="&lt;%= book.name %&gt;" target="_blank"&gt;&lt;%= image_tag book.imageurl, :width =&gt; '30px', :height =&gt; '30px'%&gt;&lt;/a&gt;
                          &lt;% end %&gt;
  
  
                    &lt;/div&gt;&lt;!--span4--&gt;
  </code></pre>
  
  <p>☆members_controller</p>
  
  <pre><code> def login
      @msg = "get"
      @member = Member.new
      @books =Group.order("created_at desc")
  
  
      if request.post? then
        @member = Member.new(params[:member])
        record = Member.find_by_user(params[:member][:user])
        if record == nil then
          @msg = "record is nil."
          @member.errors.add('user',"not registrated!")
        else
          if record.pass != params[:member][:pass] then
            @msg = "password is wrong."
            @member.errors.add('pass','password is wrong!')
          else 
            @msg = "good!"
            @member = record
            session[:user_id] = record.id
            redirect_to '/members/' + record.id.to_s and return
          end
        end
      end
        render :layout =&gt;'login'
    end
  </code></pre>