我构建了一个系统,用户只有在用户登录时才能看到组#show pages。有效。但我改变了主意。现在我希望访问者也可以看到组#显示页面,即使他们没有登录。当他们不登录时我删除了一些代码,因为会话相关代码如
#@member = Member.find(session[:user_id])
#@isGr = GroupInMember.where(:member_id => session[:user_id], :group_id =>
#params[:id].to_i).count > 0
会导致一些错误。
虽然我认为我可以避免来自会话[:user_id]的错误,但我遇到了以下错误。我该如何解决这个问题?
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".
这是我的代码;
☆groups_controller
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 => session[:user_id], :group_id =>
params[:id].to_i).count > 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 => session[:user_id], :group_id =>
#params[:id].to_i).count > 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
☆show.html.erb(groups_controller)
<div class="container-fluid">
<div class="row-fluid">
<div class="span4">
<div id="individual">
<h4>※「<%= @group.name %>」のページ</h4>
<%= link_to image_tag(@group.imageurl, :width=>"100" ,:height=>"150"),@group.detailurl , :target => '_blank' %>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>プロフィール</th>
</tr>
</thead>
<tbody>
<tr>
<th>著者</th>
<td><%= @group.author %></td>
</tr>
<tr>
<th>出版年</th>
<td><%= @group.published %></td>
</tr>
<tr>
<th>出版社</th>
<td><%= @group.publish %></td>
</tr>
<tr>
<th>ページ数</th>
<td><%= @group.page %></td>
</tr>
<tr>
<th>ISBN</th>
<td><%= @group.isbn %></td>
</tr>
</tbody>
</table>
</div><!--individual-->
<%# if @group.admin %>
<%#= link_to 'Profile Edit', edit_group_path(@group),class: "btn btn-midium btn-primary" %>
<%# end %>
<hr>
<% if session[:user_id]%>
<div class="group-side">
<% if @isGr %>
※登録済みです。
<% else %>
※登録していません。
<% end %>
</div>
<p class="group-side">
<%= link_to '本棚登録/解除', {:controller => 'groups', :action => 'join', :id =>
@group.id }, class: "btn btn-midium btn-primary"%></p>
</p>
<p></p>
<div class="group-side2"><b>この本を登録した人一覧:</b></div>
<% @group.group_in_members.each do |m| %>
<% @member = Member.find(m.member_id) %>
<% if @member.provider %>
<a href="/members/<%= m.member_id %>" rel="tooltip" title="<%= raw(@member.name) %>"><%= image_tag @member.image, :width =>'30px', :height =>'30px' %></a>
<% elsif @member.avatar_file_name %>
<a href="/members/<%= m.member_id %>" rel="tooltip" title="<%= raw(@member.name) %>"><%= image_tag @member.avatar.url(:thumb), :width =>'30px', :height => '30px' %></a>
<% else %>
<a href="/members/<%= m.member_id %>" rel="tooltip" title="<%= raw(@member.name) %>"><%= image_tag 'love.png', :width =>'30px', :height => '30px' %></a>
<% end %>
<%# if GroupInMember.where( :member_id => session[:user_id], :group_id => @group.id ).length > 0 %>
<%# end %>
<% end %>
</div>
<% end %>
<div class="span8">
<p>
<b>※ページと行数は半角数字。ページと投稿内容の入力は必須です。</b>
</p>
<div class="post_on_group">
<% if GroupInMember.where(:member_id =>session[:user_id], :group_id=>@group.id).length > 0 %>
<%= form_for([@group, @group_message]) do |f| %>
<% if @group_message.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@group_message.errors.count, "error") %> prohibited this group_message from being saved:</h2>
<ul>
<% @group_message.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%#= f.label :member_id %>
<%= f.hidden_field :member_id, :value => session[:user_id] %>
</div>
<div class="field">
<%#= f.label :group_id %>
<%= f.hidden_field :group_id %>
</div>
<div class="form_index2">
<%= f.label :"何ページ?" %>
<%= f.text_field :page, :class=> "span3" %>
</div>
<div class="form_index4">
<%= f.label :"何行目あたり?" %>
<%= f.text_field :line, :class=> "span1" %>
</div>
<div class="form_index4">
<%= f.label :"投稿内容" %>
<%= f.text_area :content ,:class => "span12",:size => "20x5" %>
</div>
<div class="actions">
<%= f.submit "投稿"%>
</div>
<% end %>
<% end %>
</div> <!--post_on_group-->
<div>
※この本に関する投稿をページ数や投稿内容から検索できます。
</div>
<div class= "form_index">
<%= form_tag({:action=>"show"}, {:method=>"get"}) do %>
<div class="from_field_index5">
<%= text_field_tag 'page' ,'', :class=> "span3" %>
<%= submit_tag 'ページ' %>
<% end %>
</div>
</div>
<div class= "form">
<%= form_tag({:action=>"show"}, {:method=>"get"}) do %>
<div class="from_field_index">
<%= text_field_tag 'content' %>
<%= submit_tag '投稿内容' %>
<% end %>
</div>
</div>
<hr>
<br/>
<% if @gms %>
<% @gms.each do |gm| %>
<div class="message_area">
<div class="each_message">
<%= image_tag gm.group.imageurl,:width => '20', :height => '25' %>
<%= 'Page:' + gm.page.to_s + '&' %><%= 'Line:' + gm.line.to_s %>
<a href="/members/<%= gm.member.id%>"><%= gm.member.name %></a>
(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gm.id%>" > <%= truncate(gm.content, { :length => 300}) %></a></p>
</div><!--group_message-->
<br/>
<% if gm.group_message_comments.present? %>
<% gm.group_message_comments.order("created_at asc").each do |gmsc|%>
<div class="group_message_comment">
<p><a href="/members/<%= gmsc.member ? gmsc.member.id : "unknown" %>"><%= gmsc.member ? gmsc.member.name : "unknown" %></a> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= truncate(gmsc.content, { :length => 300}) %>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<% end %> <!--if -->
</div><!--each_message-->
<br>
</div> <!--message_area-->
<% end %>
<% else %>
<% @group_messages.each do |gm| %>
<div class="message_area">
<div class="each_message">
<%= image_tag gm.group.imageurl,:width => '20', :height => '25' %>
<%= 'Page:' + gm.page.to_s + '&' %><%= 'Line:' + gm.line.to_s %>
<a href="/members/<%= gm.member.id%>"><%= gm.member.name %></a>
(<%= gm.created_at.strftime'%Y-%m-%d %H:%M' %>)
<div class="group_message">
<p class="message_content"><a href="/group_messages/<%= gm.id%>" > <%= truncate(gm.content, { :length => 300}) %></a></p>
</div><!--group_message-->
<br/>
<% if gm.group_message_comments.present? %>
<% gm.group_message_comments.each do |gmsc|%>
<div class="group_message_comment">
<p><a href="/members/<%= gmsc.member ? gmsc.member.id : "unknown" %>"><%= gmsc.member ? gmsc.member.name : "unknown" %></a> (<%= gmsc.created_at.strftime'%Y-%m-%d %H:%M' %>)</p>
<%= truncate(gmsc.content, {:length =>300}) %>
</div> <!--group_message_comment-->
<br/>
<% end %><!-- each do -- >
<% else %>
<% end %> <!--if -->
</div><!--each_message-->
<br>
</div> <!--message_area-->
<% end %> <!--each do -->
<% end %>
</div><!--span8-->
☆login.html.erb(members_controller)
<div class="span4">
<h4 class="title">Users' reading texts</h4>
<% @books.each do |book| %>
<a href="/groups/<%= book.id %>" rel="tooltip" title="<%= book.name %>" target="_blank"><%= image_tag book.imageurl, :width => '30px', :height => '30px'%></a>
<% end %>
</div><!--span4-->
☆members_controller
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 =>'login'
end
答案 0 :(得分:2)
在您的login
操作中,您在条件中有redirect_to
,在{...}}之外有render
。您需要以某种方式修改它以具有两个顶级条件分支:
if (...)
redirect_to ...
else
render ...
end