advanced Search (checkboxes and selection) - ruby-on-rails

I'm having a problem with my advanced search.
Essentially, its an advanced search for bands (name, style, bandpicture by checkboxes (checked: have a bandpicture))
Here is the code.
result.html.erb
<table >
<colgroup>
<col width="250" />
</colgroup>
<tr>
<td>
<label for="name">Filter</label></br>
<%= form_tag band_path do %>
<label for="Style">Style</label></br>
<%= select_tag :style_id, options_from_collection_for_select(Style.all, :id, :name, params[:style_id]), include_blank: true, class: "form-control" %>
<br>
</br>
<%= check_box_tag 'options[]', "picture" %>
<%= button_to '#', class: 'btn btn-default' do %>
<%= t(:find) %>
<% end %>
<% end %>
</td>
<td>
<div>
<% #bands.each do |band|%>
<div class="top-bar">
<div class="center">
<div class="info">
<div class="name"><%=band.id %></div>
<div> <%= band.zip %>, <%= band.city %> </div>
</div>
<div class="desc">
<table>
<tr>
<td>Styles</td>
<td>
<% band.styles.each do |s| %>
<%= s.style.name %>
<% end %>
</td>
</tr>
</table>
</div>
<% end %>
</td>
</tr>
</table>
models band.rb
def self.search1(style_id)
Profile.joins("...").where("style_id = ?", style_id)
end
controller.
def searchresult
if params[:style_id].present?
#bands = Band.search1 params[:style_id]
elsif params[:options]
Band.where(:options => #filtered_options)
else
#bands = Band.all
end
end
The searchresult don't show the result of the checkboxes and I have this errors:
undefined method `search1' for #<Class:0x00000008eb5548>

Related

How to filter status with Rails Ransack's filter?

I found a description in the Ranksack tutorial (https://activerecord-hackery.github.io/ransack/getting-started/sorting) about filters. I have used Ransack to complete the search and sorting functions. I hope to add a filter to let the user select the desired state (this is one of the columns in the table) and point out the data that matches the state. But I don't know how to transform this code. Please give me some hints, thanks.
This is the code mentioned in the tutorial
<div class="filters" id="filtersSidebar">
<header class="filters-header">
<div class="filters-header-content">
<h3>Filters</h3>
</div>
</header>
<div class="filters-content">
<%= search_form_for #q,
class: 'form',
url: articles_path,
html: { autocomplete: 'off', autocapitalize: 'none' } do |f| %>
<div class="form-group">
<%= f.label :title_cont, t('Filter_by_keyword') %>
<%= f.search_field :title_cont %>
</div>
<%= render partial: 'filters/date_title_sort', locals: { f: f } %>
<div class="form-group">
<%= f.label :grade_level_gteq, t('Grade_level') %> >=
<%= f.search_field :grade_level_gteq %>
</div>
<div class="form-group">
<%= f.label :readability_gteq, t('Readability') %> >=
<%= f.search_field :readability_gteq %>
</div>
<div class="form-group">
<i><%= #articles.total_count %> articles</i>
</div>
<div class="form-group">
<hr/>
<div class="filters-header-content">
<%= link_to request.path, class: 'form-link' do %>
<i class="far fa-undo icon-l"></i><%= t('Clear_all') %>
<% end %>
<%= f.submit t('Filter'), class: 'btn btn-primary' %>
</div>
</div>
<% end %>
</div>
</div>
This is the current code of my page
<%= search_form_for(#query, url: list_tasks_path) do |f| %>
<%= f.label :name, t('search') %>
<%= f.search_field :name_or_note_cont, placeholder: t('task_name') %>
<%= f.select :status_eq, Task.statuses.map { |key, value| [t("checkpoint_status.#{key}"), value] }, prompt: t('all_status') %>
<%= f.submit %>
<% end %>
<p class="lead"><%= t('task_list') %></p>
<table class="table table-striped table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">
<%= sort_link(#query, :name) do %>
<%= t('task_name') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(#query, :status) do %>
<%= t('task_status') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(#query, :priority) do %>
<%= t('task_priority') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(#query, :created_at) do %>
<%= t('task_created') %>
<% end %>
</th>
<th scope="col">
<%= sort_link(#query, :due_date) do %>
<%= t('task_ends') %>
<% end %>
</th>
<th scope="col"><%= t('task_actions') %></th>
<th colspan="2"></th>
<th scope="col"><%= t('task_tag') %></th>
</tr>
</thead>
<tbody>
<% #tasks.each do |task| %>
<tr>
<th scope="row"><%= task.id %></th>
<td>
<%= task.name %>
</td>
<td>
<%= t("checkpoint_status.#{task.status}") %>
</td>
<td>
<%= t("checkpoint_priority.#{task.priority}") %>
</td>
<td>
<%= task.created_at.strftime("%b %d %R") %>
</td>
<td>
<%= task.due_date.strftime("%b %d %R") %>
</td>
<td>
<%= link_to t('task_cont') , task_path(task) %>
</td>
<td>
<%= link_to t('task_do_edit') , edit_task_path(task) %>
</td>
<td>
<%= link_to t('task_do_kill') , task_path(task), method: :delete, data:{ confirm: t('task_do_sure') } %>
</td>
<td>
<%= task.tag_list %>
</td>
</tr>
<% end %>
</tbody>
</table>

NoMethodError in Spree::Admin::Reviews#index

I am new to spree and ruby on rails.While clicking the 'reviews' in spree back-end, it's throwing an error.
Thanks in Advance.
NoMethodError in Spree::Admin::Reviews#index
Showing - var/lib/gems/2.3.0/ruby/2.3.0/bundler/gems/spree_reviews-e51cae9627f9/app/views/spree/admin/reviews/index.html.erb where line #25 raised:
index.html.erb:
<% content_for :page_title do %>
<%= Spree.t('reviews') %>
<% end %>
<% render 'spree/admin/shared/product_sub_menu' %>
<% content_for :table_filter_title do %>
<%= Spree.t('search') %>
<% end %>
<% content_for :table_filter do %>
<div data-hook="admin_reviews_index_search">
<%= search_form_for [:admin, #search] do |f| %>
<div class="alpha three columns">
<div class="field">
<%= label_tag nil, Spree.t(:user) %><br />
<%= f.text_field :name_cont, :size => 25 %>
</div>
</div>
<div class="seven columns">
<div class="field">
<%= label_tag nil, Spree.t(:title) -%><br/>
<%= f.text_field :title_cont, :size => 25 -%>
</div>
</div>
<div class="four columns">
<div class="field">
<%= label_tag nil, Spree.t(:review) -%><br/>
<%= f.text_field :review_cont, :size => 25 -%>
</div>
</div>
<div class="two columns omega">
<div class="field">
<%= label_tag nil, Spree.t(:approval_status)-%><br/>
<%= f.select :approved_eq, [[Spree.t('all'), nil],
[Spree.t('approved_reviews'), true],
[Spree.t('unapproved_reviews'),false]] -%>
</div>
</div>
<div class="clear"></div>
<div class="form-buttons actions filter-actions" data-
hook="admin_reviews_index_search_buttons">
<%= button Spree.t(:search), 'icon-search' %>
</div>
<%- end -%>
</div>
<%- end -%>
<% paginate #reviews %>
<% if #reviews.any? %>
<table class="index">
<colgroup>
<col style="width: 25%;">
<col style="width: 10%;">
<col style="width: 10%;">
<col style="width: 20%;">
<col style="width: 15%;">
<col style="width: 17%;">
</colgroup>
<thead>
<tr>
<th><%= Spree.t('product') %></th>
<th><%= Spree::Review.human_attribute_name(:rating) %></th>
<th><%= Spree.t('feedback') %></th>
<th><%= Spree::Review.human_attribute_name(:user) %></th>
<th><%= Spree::Review.human_attribute_name(:created_at) %></th>
</tr>
</thead>
<tbody>
<%- #reviews.each do |review| -%>
<tr id="<%= dom_id review %>">
<td>
<% if review.product %>
<%= link_to review.product.name, product_path(review.product) %>
<% end %>
</td>
<td class="align-center">
<%= txt_stars(review.rating) %>
</td>
<td class="align-center">
<%= link_to "(#{review.feedback_stars}/#{review.feedback_reviews.size})", admin_review_feedback_reviews_path(review) %>
</td>
<td class="align-center">
<%= review.user_id ? link_to(review.user.try(:email), [:admin, review.user]) : Spree.t(:anonymous) %></p>
<p><%= Spree::Review.human_attribute_name(:ip_address) %>: <%= review.ip_address ? link_to(review.ip_address, "http://whois.domaintools.com/#{review.ip_address}") : '-' %></p>
</td>
<td class="align-center">
<%= l review.created_at, :format => :short %>
</td>
<td class="actions">
<%= link_to_with_icon 'check', Spree.t('approve'), approve_admin_review_url(review), :no_text => true, class: 'approve' unless review.approved %>
<%= link_to_edit review, :no_text => true, :class => 'edit' %>
<%= link_to_delete review, :no_text => true %>
</td>
</tr>
<% end %>
</tbody>
</table>
<% else %>
<div class="no-objects-found">
<%= Spree.t(:no_results) %>
</div>
<% end %>
<%= paginate #reviews -%>
reviews_controller
class Spree::Admin::ReviewsController <
Spree::Admin::ResourceController
helper Spree::ReviewsHelper
def index
#reviews = collection
params[:q] ||= {}
#search = Spree::Review.ransack(params[:q])
#collection = #search.result.includes([:product, :user,
:feedback_reviews]).page(params[:page]).per(params[:per_page])
end
def approve
r = Spree::Review.find(params[:id])
if r.update_attribute(:approved, true)
flash[:notice] = Spree.t("info_approve_review")
else
flash[:error] = Spree.t("error_approve_review")
end
redirect_to admin_reviews_path
end
def edit
if #review.product.nil?
flash[:error] = Spree.t("error_no_product")
redirect_to admin_reviews_path and return
end
end
private
def collection
params[:q] ||= {}
#search = Spree::Review.ransack(params[:q])
#collection = #search.result.includes([:product, :user,
:feedback_reviews]).page(params[:page]).per(params[:per_page])
end
end
review.rb
class Spree::Review < ActiveRecord::Base
belongs_to :product, touch: true
belongs_to :user, :class_name => Spree.user_class.to_s
has_many :feedback_reviews
after_save :recalculate_product_rating, :if => :approved?
after_destroy :recalculate_product_rating
validates :name, presence: true
validates :review, presence: true
validates :rating, numericality: { only_integer: true,
greater_than_or_equal_to: 1,
less_than_or_equal_to: 5,
message: Spree.t('you_must_enter_value_for_rating') }
default_scope { order("spree_reviews.created_at DESC") }
scope :localized, ->(lc) { where('spree_reviews.locale = ?', lc) }
scope :most_recent_first, -> { order('spree_reviews.created_at DESC') }
scope :oldest_first, -> { reorder('spree_reviews.created_at ASC') }
scope :preview, -> { limit(Spree::Reviews::Config[:preview_size]).oldest_first }
scope :approved, -> { where(approved: true) }
scope :not_approved, -> { where(approved: false) }
scope :default_approval_filter, -> { Spree::Reviews::Config[:include_unapproved_reviews] ? all : approved }
def feedback_stars
return 0 if feedback_reviews.size <= 0
((feedback_reviews.sum(:rating) / feedback_reviews.size) + 0.5).floor
end
def set_search
#search=Product.search(params[:q])
end
def recalculate_product_rating
self.product.recalculate_rating if product.present?
end
end
Try to add result method after ransack:
#search = Spree::Review.ransack(params[:q]).result

How can I access to nested fields attributes using nested_forms gem

I am using nested_forms gem in my project, nested_forms uses a partial view to render the line with my nested object attributes, here is my partial view code:
<tr class="fields">
<td>
<%= f.text_field :nombre, class: 'form-control craf' %>
</td>
<td>
<%
concat f.select :idtipodocumento, CrTipoDocumento.all.collect {|p| [ p.nombre, p.id ] }, {prompt: 'Seleccione el tipo de documento'}, :class=>'form-control'
%>
</td>
<td>
<%= f.text_field :documento, class: 'form-control', maxlength: '50' %>
</td>
<% if current_user.email.include? '#cifco.gob.sv' %>
<td>
<%= f.check_box :activo %>
</td>
<td>
<%= f.check_box :impresa %>
</td>
<td>
<%= f.check_box :extra %>
</td>
<% end %>
<td>
<%= f.link_to_remove raw('<i class="fa fa-minus-circle" aria-hidden="true"></i>'), :class =>'btn btn-danger hidden-print' %>
</td>
And here is the code block of the view where this partial is rendering:
<tbody>
<fieldset id="acreditados">
<%= f.fields_for :cr_acreditados, :wrapper => false %>
</fieldset>
</tbody>
So, what I want to do is evaluate if 'impresa' == false and only then render it, but if try f.impresa it says the method 'impresa' doesnt exist for f
Here is the form full code:
<%= nested_form_for(#cr_acreditacion_cliente, :validate => true) do |f| %>
<% if #cr_acreditacion_cliente.acreditaciones == nil %>
<% else %>
<table class="table table-striped table-bordered table-hover" id="AC">
<thead>
<tr>
<th>Evento</th>
<th>Cliente</th>
<th>Stand</th>
<th>Acreditaciones</th>
<th>Acreditaciones Disponibles</th>
<th>Tipo de Acreditacion</th>
</tr>
</thead>
<tbody>
<td><%= #cr_acreditacion_cliente.cr_evento.nombre %></td>
<td><%= #cr_acreditacion_cliente.cr_cliente.nombre %></td>
<td><%= #cr_acreditacion_cliente.cr_stand.codigo %></td>
<td><%= #cr_acreditacion_cliente.acreditaciones %></td>
<td>
<p hidden id="cnt"><%= #cr_acreditacion_cliente.acreditaciones %></p>
<p id="acd">
<%= #counter = #cr_acreditacion_cliente.acreditaciones - #cr_acreditacion_cliente.cr_acreditados.count %>
</p>
</td>
<td><%= #cr_acreditacion_cliente.cr_tipocre.tipo %></td>
</tbody>
</table>
<table class="table table-striped table-bordered table-hover" id="DAC">
<thead>
<tr>
<th>Nombre</th>
<th>Tipo de Documento</th>
<th>Documento</th>
<% if current_user.email.include? '#cifco.gob.sv' %>
<th>Activo</th>
<th>Impresa</th>
<th>Extra</th>
<% end %>
<th class="hidden-print">Acciones</th>
</tr>
</thead>
<tbody>
<fieldset id="acreditados">
<%= f.fields_for :cr_acreditados, :wrapper => false %>
</fieldset>
</tbody>
</table>
<div id="addBtn">
<% if #counter >= 1 %>
<p><%= f.link_to_add raw('<i class="fa fa-plus-circle" aria-hidden="true"></i>'), :cr_acreditados, id: 'btna', :class => 'btn btn-primary lta hidden-print', "data-target" => "#DAC" %></p>
<% else %>
<p class="text-center"><h3>No dispone de mas acreditaciones</h3></p>
<% end %>
</div>
<div class="actions text-center">
<%= f.submit "Guardar Credenciales", :class => 'btn btn-success hidden-print', data: { confirm: 'Favor verifique los datos antes de almacenarlos' }%>
</div>
<% end %>
<% end %>

Rails 4, check_box_tag not passing ids into params hash after ajax render

My form works the way I intended it to before I filter the results in table.
The intended functionality is, allow user to select multiple objects, the selected object id's are submitted in the params hash, and accessible via params[:event_ids], when the user clicks the 'Submit' button (I've re-labeled the 'submit' button to 'swap' to clarify functionality for the user")
When I filter the results via ajax, the checkboxes and the submit button are still visible in the view, but when I click 'swap', the selected objects are not being submitted with form. I.E params[:event_ids] is not present in the params hash. I'm stumped.
Here is the code:
show.html.erb
<div class="jumbotron">
<div class="container">
<h1><%=#carpool.name%> Carpool!</h1>
<p>View carpool specific events; Send Invites and Add Events if you are the carpool manager; View a list of all members in the carpool</p>
<% if current_user == #carpool.manager %>
<p>
<a class="btn btn-primary btn-lg" href="<%=#carpool.id%>/events/new" role="button">Add Events</a>
<a class="btn btn-primary btn-lg" href="<%=#carpool.id%>/invites/new" role="button">Send Invites</a>
</p>
<% end %>
</div>
</div>
<div class="container">
<div class="row">
<div class="col-md-3">
<%= render 'show_members_list' %>
</div>
<div class="col-md-9">
<%= render 'show_events_list' %>
</div>
</div>
</div>
_show_events_list.html.erb
<div class="panel panel-default">
<!-- Default panel contents -->
<div class="panel-heading">Drive Events</div>
<div class="panel-body">
<div class="btn-group right" role="group" aria-label="...">
<%= button_to "All Drive Events",
carpool_path(#carpool) + '?search=all',
type: "button",
remote: true,
method: :get,
class: "btn btn-default" %>
<%= button_to "I'm Driving",
carpool_path(#carpool) + '?search=my_events',
type: "button",
remote: true,
method: :get,
class: "btn btn-default"
%>
<%= button_to "I'm being Picked Up",
carpool_path(#carpool) + '?search=not_driving',
type: "button",
remote: true,
method: :get,
class: "btn btn-default"
%>
</div>
<div class="swap_button"><%= submit_tag 'Swap!', data: {confirm: "Are you sure?"}, class: "btn btn-primary btn-lg swap", id: "swap_submit" %></div>
</div>
<!-- Table -->
<table class="table">
<thead>
<tr>
<th>Who's Driving?</th>
<th>Event Name</th>
<th>Drive Event Type</th>
<th>Place</th>
<th>Game/Not Game</th>
<th>Address</th>
<th>Date</th>
<th>Swap</th>
</tr>
</thead>
<%= form_tag carpool_path(#carpool), method: :get, id: "swap_form" do %>
<tbody id="carpool_events">
<%= render 'events_list.html.erb', locals: {events: #events} %>
</tbody>
<% end %>
</table>
</div>
_events_list.html.erb
<% if #events != nil %>
<% #events.each do |event| %>
<% if event.driver_id != nil %>
<tr>
<td><%= event.driver ? event.driver.name : "swapped"%></td>
<td><%= event.name%></td>
<td><%= event.drop_off? ? "Drop Off At" : "Pick Up From" %></td>
<td><%= event.location_name%></td>
<td><%= event.is_game ? "Game" : "Not Game(event, practice)" %></td>
<td><%= event.location_address %></td>
<td><%= event.date ? event.date.strftime("%A, %d %b %Y %l:%M %p") : "Please Enter Details on TeamSnap!"%></td>
<td>
<% if current_user == event.driver %>
<%= check_box_tag "event_ids[]", event.id %>
<% end %>
</td>
</tr>
<% end %>
<% end %>
<% end %>
show.js.erb
$("#carpool_events").html("<%= escape_javascript(render partial: 'events_list', locals: { events: #events } ) %>");
trying something like this
<%= check_box_tag "carpool[event_ids][]", event.id %>
and your console logging verify the params that will appear

if !checklogin? then return end

I would like to make show.html.erb(groups) pages open to visitors(,which are not users) as well.
But my code has "if !checklogin? then return end" on groups_controller, so they can't see the show.html.erb pages. I know I should remove "if ! checklogin? then return end" but have no idea how to reconstruct my code as a whole.
Could you give me some hints?
☆members_controller
def checklogin?
if session[:user_id] != nil then
return true
else
redirect_to '/members/login'
return false
end
end
☆login.html.erb(members_controller)
<div class="span4">
<h4 class="title">Users' reading texts</h4>
<% #books.each do |book| %>
<%= image_tag book.imageurl, :width => '30px', :height => '30px'%>
<% end %>
</div><!--span4-->
☆groups_controller
def show
#group = Group.find(params[:id])
#group_message = Group.find(params[:id]).group_messages.build
if !checklogin? then return end
#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
end
☆show.html.erb(groups)
<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 %>
<%= image_tag #member.image, :width =>'30px', :height =>'30px' %>
<% elsif #member.avatar_file_name %>
<%= image_tag #member.avatar.url(:thumb), :width =>'30px', :height => '30px' %>
<% else %>
<%= image_tag 'love.png', :width =>'30px', :height => '30px' %>
<% 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=> "span2" %>ページ(※半角数字の入力必須)
</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 %>
<%= gm.member.name %>
(<%= 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><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= 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 %>
<%= gm.member.name %>
(<%= 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><%= gmsc.member ? gmsc.member.name : "unknown" %> (<%= 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-->
The way to check login and redirect, is by using the callback of before_filter
before_filter :check_login
check_login will redirect user to login action unless redirect
def check_login
unless session[:user_id]
flash[:error] = "You must be logged in to access this section"
redirect_to '/members/login'
end
end
If you want to enable access just for some actions - you can exclude them:
before_filter :check_login, :except => [:index, :show, etc..]

Resources