Ordering by category id and price on index page - ruby-on-rails

I’m having trouble with ordering my houses. I have a houses that belong_to category (category has_many houses) and I want to order the houses on the index based on their category_id, but whatever I try it doesn’t seem to change the order. It’s a mystery to me. Maybe you could help me out?
class HousesController < ApplicationController
skip_before_action :authenticate_user!, only: [:index, :show]
before_action :set_house, only: [ :show, :edit, :update, :destroy, :inquiry ]
​
def index
# byebug
#houses = policy_scope(House).order(:name)
#houses = params.has_key?(:h) ? index_result(#houses) : #houses
# split_categories(#houses)
end
​
def index_result(houses)
# byebug
wall_thickness = params[:h].to_i
if wall_thickness == 1
houses.order(:name)
render "results_index_all"
elsif wall_thickness == 28 || 40
houses = houses.where(:wall => wall_thickness)
houses.order(:name)
render "results_index"
else
end
end
​
def split_categories(houses)
# #houses_1 = #houses.where(price_cents: 0...260000)
# #houses_2 = #houses.where(price_cents: 260000..1000000)
# byebug
category = Category.find_by(id: params[:category])
houses = #category.present? ? category.houses.order(price_cents: :asc) : houses
end
​
def show
end
​
def new
#house = House.new
authorize #house
end
​
def create
#house = House.new(house_params)
#house.user = current_user
authorize #house
if #house.save
redirect_to house_path(#house)
else
render :new
end
end
​
def edit
end
​
def update
if #house.update(house_params)
redirect_to house_path(#house)
else
render :edit
end
end
​
def destroy
#house.destroy
redirect_to root_path
end
​
private
​
def set_house
#house = House.friendly.find(params[:id])
authorize #house
end
​
def house_params
params.require(:house).permit(:sku, :name, :price_cents, :width, :length, :wall, pictures: [])
end
​
end
Below is the normal index where I split houses up in categories starting with category_id: 2 (normal garden houses), then 1 (gazebos) and finally 3 (garages). This is partial which is rendered app/views/houses/index.html.erb
<% if !#category %>
<%= render 'first-banner-home' %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.where(category_id: 2).each do |house| %>
<div class="col-sm-6 col-md-4">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<div class="container catalog">
<h3><%= t('home.gazebos') %></h3>
<div class="row">
<% #houses.where(category_id: 1).each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<div class="container catalog">
<h3><%= t('home.garages') %></h3>
<div class="row">
<% #houses.where(category_id: 3).each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% elsif params[:h].present? %>
<%= render "results_index" %>
<% else %>
<%= render "category_index" %>
<% end %>
This is the index where the houses are filtered based on their wall thickness (see in app/controllers/houses_controller.rb ). This is where I want the house to order based on their category_id, which doesn’t work at the moment.
<% content_for(:title) do %><%= t('topbar.brand') %><% end %>
<% content_for(:og_site_name) do %><%= t('topbar.brand') %><% end %>
<% content_for(:description) do %><%= t('garden_houses.meta_description') %><% end %>
<% content_for(:og_image) do %><%= cl_image_path("https://res.cloudinary.com/www-ibizagardenhouses-com/image/upload/c_scale,h_630,w_1200/v1558696079/ibi-3/image.jpg") %><% end %>
<% content_for(:og_description) do %><%= t('garden_houses.meta_description') %><% end %>
<%= render 'first-banner-results' %>
<% if #houses.count >= 4 %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6 col-md-4">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% else %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% end %>
This is exactly the same html as app/views/houses/results_index.html.erb , but where I show all the houses (params[h: 1]) in a layout that’s not the same as the home page. Same problem here as on the filtered results page.
<% content_for(:title) do %><%= t('topbar.brand') %><% end %>
<% content_for(:og_site_name) do %><%= t('topbar.brand') %><% end %>
<% content_for(:description) do %><%= t('garden_houses.meta_description') %><% end %>
<% content_for(:og_image) do %><%= cl_image_path("https://res.cloudinary.com/www-ibizagardenhouses-com/image/upload/c_scale,h_630,w_1200/v1558696079/ibi-3/image.jpg") %><% end %>
<% content_for(:og_description) do %><%= t('garden_houses.meta_description') %><% end %>
<%= render 'first-banner-all' %>
<% if #houses.count >= 4 %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6 col-md-4">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% else %>
<div class="container catalog">
<%= render "filter_buttons" %>
<div class="row">
<% #houses.each do |house| %>
<div class="col-sm-6">
<div class="card">
<% if house.pictures? %>
<div class="imageTop" style="position: relative;">
<%= cl_image_tag(house.pictures[0], :quality=>50, :width=>1800, :crop=>"scale", class: "card-img-top-alt", alt: house.name + " " + house.category.name) %>
<%= link_to "", house_path(house), class: "card-link" %>
</div>
<% end %>
<div class="card-body indexBody">
<div class="card-price">
<h3 class="card-title"><%= house.name %></h3>
<h3><%= house.length.to_f / 100 %> x <%= house.width.to_f.to_f / 100 %> m</h3>
</div>
<div class="card-price">
<% if house.price_present == false %>
<h3><%= t('show.consult') %></h3>
<% else %>
<h3><%= humanized_money_with_symbol(house.original_price) %><span style="color: red"> - 15%</span> = <%= humanized_money_with_symbol(house.price) %></h3>
<% end %>
</div>
<% if policy(house).update? %>
<%= link_to "Edit", edit_house_path(house) %>
<% end %>
<% if policy(house).destroy? %>
<%= link_to "Remove", house_path(house), method: :delete, data: { confirm: "Are you sure?" } %>
<% end %>
</div>
</div>
</div>
<% end %>
</div>
</div>
<%= render "last-banner" %>
<% end %>
As you can see, there's a lot of duplication of code (not very DRY) and so I'd like a little help in getting more DRY and have the houses be ordered by category_id and then by price.
I'd appreciate your help.

It's not clear to me what you tried so far, but if all you need is to sort by category_id, I think you can define a scope within your house model as follow:
scope :sort_by_category_id_asc, ->{
order(arel_table[:category_id].asc)
}
Then you can use that scope as follow: House.where(...).sort_by_category_id_asc, also you can use desc, depends on how you want to order the result.
Hope this helps! 👍

I created a method that can sort houses by its category_id. If you want to sort it by other things just change category_id to your desired thing. You can sort it ascending or descending order. Here it is:
def sort_by_category_id(houses, order)
case order_by
when "asc"
houses = houses.sort_by{ |house|
house[:category_id]
}
when "desc"
house= house.sort_by{ |house|
house[:category_id]
}.reverse!
end
house
end
Don't forget to call this method wherever you want it to be worked. Below code will sort your houses by category_id and save it in sorted_houses variable. I
sorted_houses_asc = sort_by_category_id(#houses, "asc")
sorted_houses_desc = sort_by_category_id(#houses, "desc")
Hope this helped you somehow.

Related

Rails 7 turbo_frame inline editing Response has no matching turbo-frame

Hello I have an rails 7 application where i want to inline edit an action item, and on the action_item.status turbo frame I get this error Response has no matching <turbo-frame id="status_action_item_8c36e8a6-70a5-4417-864b-198788f984b3"> element
I have several identical frames(different domID) which have no issue.
I have been following along with some great turbo content from gorails and hotrails and this is really puzzeling me, i am hoping i am close and it just needs a set of eyes.
Index
<div class="row justify-content-center">
<div class="col-12">
<div class="card shadow border-dark border-2 rounded-2">
<div class="card-header bg-dark border-secondary border-bottom border-2 d-flex justify-content-between align-items-center">
<div class="dropdown">
<button class="btn btn-lg btn-outline-light dropdown-toggle float-start" type="button" id="dropdownMenuButton1" data-bs-toggle="dropdown" aria-expanded="false" target="_top">
Incomplete Tasks
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenuButton1">
<li><%= link_to "Completed Tasks", completed_action_items_path, class: "dropdown-item"%></li>
<li><%= link_to "In Progress Tasks", in_progress_action_items_path, class: "dropdown-item"%></li>
</ul>
</div>
<%= link_to new_action_item_path, class: 'btn btn-outline-light float-end m-2' do %>
+
<% end %>
</div>
<div class="card-body m-0 p-0">
<div class="table-responsive">
<table class="table table-hover table-borderless m-0 p-0 align-middle">
<thead>
<tr class="table-dark">
<th style="width: 3%"></th>
<th class="" style="width: 30%"><span class="m-2">Name</span></th>
<th class="d-none d-md-table-cell" style="width: 27%"><span class="m-2">Meeting</span></th>
<th class="text-center" style="width: 10%"><span class="m-2">Status</span></th>
<th class="text-center d-none d-md-table-cell" style="width: 5%"><span class="m-2">Priority</span></th>
<th class="text-center" style="width: 5%"><span class="m-2">Due</span></th>
<th class="text-center" style="width: 10%"><span class="m-2">Actions</span></th>
</tr>
</thead>
<tbody>
<% #action_items.each do |action_item| %>
<tr>
<td>
<button class="btn btn-sm btn-outline-success rounded-3 border-0 float-end me-1"
type="button"
data-bs-toggle="tooltip"
data-bs-placement="top"
data-bs-custom-class="custom-tooltip"
title="Complete task"
>
<i class="fas fa-check-circle text-success hide"></i>
</button>
</td>
<td class=""><small class="ms-2"><%= link_to action_item.name, action_item, class: "text-decoration-none text-dark" %></small></td>
<td class="d-none d-md-table-cell"><small class=""><%= action_item.meeting.name %></small></td>
<%= render 'status', action_item: action_item %>
<%= render 'priority', action_item: action_item %>
<%= render 'due', action_item: action_item %>
<%= render 'actions', action_item: action_item %>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
Status Partial
<td class="text-center">
<small class="">
<% frame_id = dom_id(action_item, "status") %>
<%= form_with model: action_item, class: '', data: { turbo_frame: frame_id } do %>
<%= turbo_frame_tag frame_id, class: 'inline-edit' do %>
<%= status_method(action_item) %>
<% end %>
<% end %>
</small>
</td>
Form
<%= form_with(model: action_item) do |form| %>
<% if action_item.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(action_item.errors.count, "error") %> prohibited this action_item from being saved:</h2>
<ul>
<% action_item.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="mb-3">
<% frame_id = dom_id(action_item, "name_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.text_field :name, class: 'form-control mw-100', placeholder: 'Description' %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<% if #Meeting.present? %>
<%= form.hidden_field :meeting_id, value: #meeting.id %>
<% end %>
<% frame_id = dom_id(action_item, "attendee_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<% if #action_item.meeting.present? %>
<div class="input-group">
<%= form.select :attendee_id, action_item.meeting.attendees.all.map { |p| [ p.user.email, p.id ] }, { include_blank: true}, { class: 'form-select' } %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
<%= form.hidden_field :status, value: "assigned" %>'
</div>
<% end %>
<% end %>
<div class="mb-3">
<% frame_id = dom_id(action_item, "action_item_priority_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.select(:priority, ActionItem.priorities.keys.map { |priority| [priority.titleize, priority]}, { include_blank: "Priority" }, { class: 'form-select' }) %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<div class="mb-3">
<% frame_id = dom_id(action_item, "status") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.select(:status, ActionItem.statuses.keys.map { |status| [status.titleize, status]}, { include_blank: "Status" }, { class: 'form-select'}) %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<div class="mb-3">
<% frame_id = dom_id(action_item, "action_item_due_date_turbo_frame") %>
<%= turbo_frame_tag frame_id do %>
<div class="input-group">
<%= form.date_field :due, class: 'form-control' %>
<%= form.button class: "btn btn-dark inline-action" do %>
<i class="fa-regular fa-floppy-disk"></i>
<% end %>
</div>
<% end %>
</div>
<div class="mb-3">
<% if action_item.persisted? %>
<div class="float-end">
<%= link_to 'Destroy', action_item, method: :delete, class: "text-danger", data: { confirm: 'Are you sure?' } %>
</div>
<% end %>
<%= form.submit class: 'btn btn-primary' %>
<% if action_item.persisted? %>
<%= link_to "Cancel", action_item, class: "btn btn-link" %>
<% else %>
<%= link_to "Cancel", action_items_path, class: "btn btn-link" %>
<% end %>
</div>
<% end %>
ActionItemsHelper
def status_method(action_item)
if action_item.status.present?
link_to action_item.status.titleize, edit_action_item_path(action_item), class: "btn btn-sm btn-outline-dark border-0"
else
link_to edit_action_item_path(action_item), class: "btn btn-sm btn-outline-dark border-0" do
content_tag(:i, "", class: ["fas", "fa-traffic-light", "fa-sm"] )
end
end
end
Thanks in advance

acts-as-taggable-on gem is not persisting tag strings into edit or into show

I have tags on my video objects. I can submit tags when creating a new video via my form however they don't appear on my video show page, video index or video edit form, which makes me think they are not being presisted to the database but I'm unclear on what I as missing or doing wrong.
models/video.rb
class Video < ActiveRecord::Base
after_create { validates_presence_of :user, :post }
belongs_to :user
belongs_to :post
has_many :comments
validates :title, presence: true
acts_as_taggable
end
views/videos/index.html.erb
<%= will_paginate #videos %>
<div class="" id="tag_cloud">
<% tag_cloud Video.tag_counts.sort { |x, y| x.name <=> y.name }, %w[s m l] do |tag, css_class| %>
<%= link_to tag.name, tag_path(tag.name), class: css_class %>
<% end %>
</div>
<div class="">
<% #videos.each do |video| %>
<div class="">
<%= link_to video.title, video_path(video) %></br >
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{video.embed_id}") %>
<p>Created by: <%= video.user.username %>, at: <%= video.created_at.strftime("%e, %b, %Y, %H:%M:%S %p") %></p>
<% unless video.comments.last.nil? %>
<%= video.comments.last.body %>
<% end %>
<p>
Tags: <%= raw video.tag_list.map { |t| link_to t, tag_path(t) }.join(", ") %>
</p>
<% end %>
</div>
</div>
views/videos/_new.html.erb
<div class="">
<% if current_user == #post.user || current_user.admin %>
<h3>Add video</h3>
<div class="">
<%= form_for [#post, #video] do |f| %>
<div class="">
<%= f.label(:title, "Video Title") %></br >
<%= f.text_field(:title) %>
</div>
<div class="">
<%= f.label(:url, "Video Url") %></br >
<%= f.text_field(:url) %>
</div>
<div class="">
<%= f.label(:tag_list, "Tags (separated by commas)") %></br >
<%= f.text_field(:tag_list, multiple: true) %>
</div>
<div class="">
<%= f.submit("Submit Video") %>
</div>
<% end %>
</div>
<% end %>
</div>
views/videos/show.html.erb
<div class="video-show">
<%= #video.title %>
<%= content_tag(:iframe, nil, src: "//www.youtube.com/embed/#{#video.embed_id}") %>
<p>
Tags: <%= #video.tag_list %>
</p>
<% if current_user == #video.user %>
<%= link_to "Edit Video", edit_video_path(#video) %> |
<%= link_to "Delete Video", video_path(#video), method: :delete, data: { confirm: "Are you sure?" } %> |
<% end %>
<% if current_user == #video.user %>
<%= link_to("Back to Log", post_path(#post)) %> |
<% end %>
<%= link_to("Back to Index", videos_path) %>
</div>
controllers/videos_controller.rb
def index
if params[:tag]
#videos = Video.tagged_with(params[:tag]).page(params[:page]).per_page(10).order(created_at: "desc")
else
#videos = Video.all.page(params[:page]).per_page(10).order(created_at: "desc")
end
end
private
def video_params
params.require(:video).permit(
:title,
:url,
:tag_list,
)
end

rails Display only the first 50 characters field in view

Display only the first 50 characters <%= news.header %>
view:
<% #news.each do |news| %>
<div class="news">
<div class="title"> <%= news.title %> </div>
<div class="photo"><%= image_tag( "1.jpg", :width =>247, :crop => :fit) %> </div>
<div class="header"> <%= news.header %></div>
<div class="cl"></div>
</div>
<% end %>
controller:
def index
#news=News.all
end
<%= news.header.truncate(50) %>
See http://api.rubyonrails.org/v4.2.7/classes/String.html#method-i-truncate

Rails 4 - Displaying a Users Search Results to a View

I'm using the Spotify API and am trying display search results for an artist on the same view page that the search form is on. I think I'm close but just need a little bit of help. Rails noob obviously..
How do I set the url http://localhost:3000/search?query=what_do_i_set_this_to??? to the user input I'm trying to collect via this line in my search form <%= text_field_tag(:query, "", class: 'form-control', autofocus: true, placeholder: "Enter artist name") %>
Much thanks in advance! I've been stuck on this for about a week now.
UPDATE (what my code currently looks like now)
Controller
artists_controller.rb
class ArtistsController < ApplicationController
def index
request = search_params[:query]
unless request.nil?
response = HTTParty.get("https://api.spotify.com/v1/search?q="+request+"&type=artist")
#hash_version = JSON.parse(response.body)
end
end
private
def search_params
params.permit(:query, :utf8, :commit)
end
end
View
artists/index.html.erb
<h1>Artist Search</h1>
<i>(search for artists you would like to follow)</i>
<br>
<br>
<%= form_tag(artists_path, method: :get) do %>
<div class="form-group">
<%= label_tag(:query, "Search artist by name:") %>
<%= text_field_tag(:query, params[:query], class: 'form-control', autofocus: true, placeholder: "Enter artist name") %>
</div>
<div class="form-group">
<%= submit_tag("Search", class: 'btn btn-success') %>
</div>
<% end %>
<% #hash_version["artists"]["items"].each_with_index do |band, index| %>
<% if index == #hash_version["artists"]["items"].size - 1 %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<!-- no hr with last li -->
</li>
<% else %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<hr class="artists_hr">
</li>
<% end %>
<% end %>
You should us the name option and passing it in the value option as params[:query] as follow :
<h1>Artist Search</h1>
<i>(search for artists you would like to follow)</i>
<br>
<br>
<%= form_tag(artists_path, method: :get) do %>
<div class="form-group">
<%= label_tag(:query, "Search artist by name:") %>
<%= text_field_tag(:query, params[:query], class: 'form-control', autofocus: true, placeholder: "Enter artist name") %>
</div>
<div class="form-group">
<%= submit_tag("Search", class: 'btn btn-success') %>
</div>
<% end %>
<% unless #hash_version.nil? %>
<% #hash_version["artists"]["items"].each_with_index do |band, index| %>
<% if index == #hash_version["artists"]["items"].size - 1 %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<!-- no hr with last li -->
</li>
<% else %>
<li class="artist_li">
<%= image_tag(band["images"].first["url"], class: 'img-responsive artist_img') rescue image_tag("microphone.png", class: 'img-responsive artist_img mic_bg') %>
<br>
<h1 class="artist_name"><%= band["name"] %></h1>
<br>
<% if band["genres"].empty? %>
<h5>no genres</h5>
<% else %>
<h5><%= band["genres"] %></h5>
<% end %>
<br>
<h5><%= band["followers"]["total"] %> followers</h5>
<br>
<h4>(Follow)</h4>
<hr class="artists_hr">
</li>
<% end %>
<% end %>
<% end %>
then in your controller just look for params[:query] and verify that it has some data before sending the request as follow :
class ArtistsController < ApplicationController
def index
request = search_params[:query]
unless request.nil?
response = HTTParty.get("https://api.spotify.com/v1/search?q="+request+"&type=artist")
#hash_version = JSON.parse(response.body)
end
end
private
def search_params
params.permit(:query, :utf8, :commit)
end
end
and you should be good :)

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