OK, I'm very new to Rails, so please be patient :)
I'm not sure how to structure the def search in the page controller so that it returns back the correct results (right now the results are blank).
I'm adapting a course code to my project. The original course is showing how to search for room by address rather than through the drop down attribute selection. It was also ordering the results by distance rather than a price. Here is what I come up with for my project:
def search
# STEP 1
if params[:search].present? && params[:search].strip != ""
session[:loc_search] = params[:search]
end
# STEP 2
if session[:loc_search] && session[:loc_search] != ""
#rooms_type = Room.where(active: true).order(session[:loc_search], order: 'price')
else
#rooms_type = Room.where(active: true).all
end
# STEP 3
#search = #rooms_type.ransack(params[:q])
#rooms = #search.result
#arrRooms = #rooms.to_a
end
I'm pretty sure that there are several mistakes in the def search.. could someone help me to figure this out?
Here are my Room model attribute.
class CreateRooms < ActiveRecord
def change
create_table :rooms do |t|
t.string :type
t.timestamps
end
end
end
Search widget on the homepage is structured in the following way:
<%= form_tag search_path, method: :get do %>
<div class="row">
<div class="col-md-7">
<%= select_tag :type, options_for_select([['Single', 1], ['Double', 2]]), ,class: "form-control" %>
</div>
<div class="col-md-2">
<%= submit_tag "Search", class: "btn btn-normal btn-block" %>
</div>
</div>
<% end %>
The search page does the following:
<div class = "row">
<% #rooms.each do |room| %>
<div class = "col-md-4">
<div class = "panel panel-default">
<div class = "panel-heading preview">
<% image_tag room.cover_photo (:medium) %>
</div>
<div class = "panel-body">
<% link_to room.type, room %>
</div>
</div>
</div>
<% end %>
</div>
Thank you.
UPDATED code based on #crispychicken feedback - see the screenshots enter image description here
First Issue: The content of your model is wrong. It belongs into a migration. Also, the active and price attributes are missing. And I think the attribute type is protected. You should rename it to something like RoomType.
To create the migration:
rails g migration create_rooms room_type:string active:boolean price:integer
And then do rails db:migrate
Second Issue:
In your controller, change the code in your Step 2 to:
#rooms_type = Room.where(active: true, room_type: session[:loc_search]).order(:price)
And you have to change type to room_type everywhere in your files.
Related
I need to set up a feature where the index page of freelancers can be filtered to only display based on a range on the attribute of price. This needs to be done from the user side, i was thinking two input boxes and a submit button.
How do I go about doing this? I checked out the has_scope gem, but I'm worried it's going to mess up my already defined order of freelancers, where they are ordered by the attribute featured (boolean). Essentially the order of the freelancers must stay the same, but then a filter must be added to that ordered object, to only show the freelancers in that price range.
There must be a way to do this but I'm very unsure. I'm going to do a bit of a code dump below, can someone point me in the right direction and show me how?
class HomeController < ApplicationController
before_action :set_freelancer, only: %i[ show edit update destroy ]
def index
#pagy, #freelancers = pagy(Freelancer.order(featured: :desc))
end
private
# Use callbacks to share common setup or constraints between actions.
def set_freelancer
#freelancer = Freelancer.find(params[:id])
end
end
My home controller
<% if user_signed_in?%>
<%== pagy_nav(#pagy)%>
<div class="d-flex flex-wrap justify-content flex-shrink-1 gap-3">
<%#freelancers.each do |freelancer| %>
<div class="card mb-3" style="max-width: 540px">
<div class="row g-0">
<div class="col-md-4">
<img src="https://i.pinimg.com/originals/57/f5/da/57f5da08bd8f52bc2d3e4ebadd67b642.jpg" class="img-fluid rounded-start" alt="6-logo">
</div>
<div class="col-md-8">
<div class="card-body">
<%= render freelancer %>
<%= link_to "Show this freelancer", freelancer, class: "btn btn-info" %>
</div>
</div>
</div>
<br/>
</div>
<%end%>
<% else %>
<h1>Please sign up, or sign in!</h1>
<%end%>
My webpage outputting each freelancer, ive also added my github itself below, im aware ive asked alot for help lately here but i am learning alot so i appreciate it
https://github.com/LCzoboriek/freelancer-assignment
(UPDATE)
So ive now added this code below to my home_controller.rb
def index
freelancers_scope = Freelancer.order(featured: :desc)
freelancers_scope = freelancers_scope.where("cost >= ?", params[:cost_lower_than]) if params[:cost_greater_than].present?
freelancers_scope = freelancers_scope.where("cost <= ?", params[:cost_greater_than]) if params[:cost_lower_than].present?
#pagy, #freelancers = pagy(freelancers_scope)
end
and this to my views, but its throwing an error saying undefined local variable or method `index' for #ActionView::Base:0x00000000013178. How do i point my form to that freelancers_scope part in my controller?
<%= form_for(index) do |f| %>
<%= f.input :cost_greater_than%>
<%= f.input :cost_lower_than%>
<%= submit_tag("Submit") %>
<% end %>
i was thinking two input boxs and a submit button.
this sounds absolutely fine
this is a very basic thing and you don't really need a gem for that, how about:
def index
freelancers_scope = Freelancer.order(featured: :desc)
freelancers_scope = freelancers_scope.where("cost >= ?", params[:cost_greater_than]) if params[:cost_greater_than].present?
freelancers_scope = freelancers_scope.where("cost <= ?", params[:cost_greater_than]) if params[:cost_lower_than].present?
#pagy, #freelancers = pagy(freelancers_scope)
end
if you really want a scope you could later define it in the Freelancer model so you wouldn't have to call where directly in the controller
I'm new to Rails and I'm using translate to post here.
I'm practicing rails and for that I set up a poll (voting) system.
Until then the voting is working normally, the votes are stored correctly, however, I would like to set up a screen on the frontend to display the result, and that's where I'm having difficulty.
How can I sum the votes from the PollOptions table of the "Votes" column. I would like to display it like this:
name: 10 votes or X% of votes
name: 10 votes or X% of votes
name: 10 votes or X% of votes
As I understand it, I need to create a method in the controller that will do this sum?
Any documentation links/tips, I'd appreciate it.
polls_controller
class PollsController < ApplicationController
def index
#poll = Poll.where(finished_at: nil).last
#poll_options = #poll.poll_options.order :name
end
def vote
#poll_option = PollOption.find params[:poll_option_id]
#poll_option.increment! :votes
respond_to do |format|
format.turbo_stream { render :vote }
end
end
end
model:
poll
class Poll < ApplicationRecord
has_many :poll_options
end
poll_option
class PollOption < ApplicationRecord
belongs_to :poll
end
In this view, I would like to call the result of the vote.
view _poll.html.erb
<div id="poll-container" class="col-lg-8 mx-lg-auto">
<div class="card shadow">
<div class="card-header">
<h2 class="card-title text-center">
<%= #poll.name %>
<p>
<%= #poll_options.pluck(:name).to_sentence(last_word_connector: ' ou ') %>
</h2>
</div>
<div class="card-body">
<div class="d-flex flex-column bd-highlight mb-3">
<% #poll_options.each do |item| %>
<p>
<%= button_to vote_url,
method: :post,
class: "btn btn-outline-secondary",
params: { poll_option_id: item.id } do %>
<h2>
<%= item.name %>
<%= image_tag item.photo, size: "40x40" %>
</h2>
<% end %>
<% end %>
</div>
</div>
</div>
</div>
https://i.stack.imgur.com/JQfQP.png
Thanks in advance for any guidance.
You can use ActiveRecord::Calculations sum (.sum(:votes)) to sum all the rows for the votes column under poll_options.
To find all the votes of a poll you need the following
#poll.poll_options.sum(:votes)
Then to create the % for the PHP for example you could do
total_votes = #poll.poll_options.sum(:votes)
php_votes = #poll.poll_options.where(name: 'PHP').votes
(php_votes / total_votes).to_i * 100
I have created a nice little view but it has some code in it that I would normally place in a controller. I have tried making a controller for the model but Active Admin ignores it. I have tried a small controller do block inside the Active Admin resource file which just caused errors. Where can I place this so that the supply_company Active Admin controller can access it?
<% supply_company_id = SupplyCompany.find(params[:id])
#ccarray = CompanyScore.where(supply_company_id: supply_company_id).pluck(:score) %>
The below partial works fine (yes i know ugly) but I can seem to find the spot to place the logic for it to be rendered in ActiveAdmin.
render partial
/app/views/admin/_supply_company_ratings.html.erb
<%
supply_company_id = SupplyCompany.find(params[:id])
#ccarray = CompanyScore.where(supply_company_id: supply_company_id).pluck(:score) %>
<% #ccarrayaverage = (#ccarray.inject 0, :+) / #ccarray.count%>
<% #ccarraypercent = (#ccarrayaverage.to_f)/10*100 %>
<% #divpercent = #ccarraypercent.to_s %>
Average Score
<h1> <%= #ccarrayaverage %></h1>
Total Score
<h1> <%= #ccarray.inject 0, :+ %></h1>
Total Votes
<h1> <%= #ccarray.count %> </h1>
Percent
<h1> <%= #ccarraypercent %> </h1>
<div class="progress">
<div class="progress-bar progress-bar-custom" role="progressbar" aria-valuenow="<%=#divpercent%>"
aria-valuemin="0" aria-valuemax="100" style="width:<%=#divpercent%>%">
<%= #divpercent.to_s %>% Percent
</div>
</div>
Maybe because its in a sidebar its not reading the activeadmin controller block.
/app/admin/supply_company.rb
show title: :company_name do
attributes_table do
row :company_name
end
end
sidebar "Products", only: :show do
attributes_table_for supply_company do
# row rating_for SupplyCompany, "price"
row :company_name
row "Ratings" do
render 'supply_company_ratings'
end
row :products do |pt|
pt.products.collect {|c| link_to c.product_name.capitalize, admin_products_path + "\/" + c.id.to_s}.join(", ").html_safe
end
end
end
Putting the code into AA controller block is right idea.
controller do
def index
supply_company_id = SupplyCompany.find(params[:id]).id # note .id part - it makes sure you pass an id not the whole object in the query below
#ccarray = CompanyScore.where(supply_company_id: supply_company_id).pluck(:score)
end
end
My Chefs Model is linked to my Meals Model through chef_id. I am displaying information about the chef, and would also like to show information about his meals. I have not been able to figure out the correct way to call it in the controller. Any ideas?
chef controller:
def index
#chefs=Chef.paginate(page: params[:page])
#meals = ????? Cant figure out how to call this
end
Index.html.erb
<div class = 'chef'>
<div class = 'row'>
<% #chefs.each do |chef| %>
<%= render chef %>
<% end %>
</div>
</div>
<%= will_paginate %>
_chef partial
<div class = 'span4'>
<div class = 'chef-box'>
<h2><center><%= link_to chef.name, chef %></center></h2>
<h2><center>"<%= chef.description %>"</center></h2>
<h2><center><%= THIS IS WHERE I WANT TO DISPLAY INFORMATION ON MEALS %></center></h2>
</div>
</div>
If you just want to access the #meals, than you can get it just by for an instance of #chef:
#meals = #chef.meals
given that you have defined the association in the Chef class like following:
class Chef < ActiveRecord::Base
has_many :meals
#rest of the code
end
Have a look at Active Record Associations here.
So, Here I am just giving an example of how to use it in views, you may have to correct it:
in _chef partial
<div class = 'span4'>
<div class = 'chef-box'>
<h2><center><%= link_to chef.name, chef %></center></h2>
<h2><center>"<%= chef.description %>"</center></h2>
<% chef.meals.each do |meal| %>
<h2><center><%= meal.information %></center></h2>
<% end %>
</div>
</div>
You will need to call correct attribute of meal in above sample code.
As previously mentioned – first and foremost make sure the relation is set up in your chef model:
class Chef < ActiveRecord::Base
has_many :meals
end
Then you can call any Chef.meals, whether that's in your controller as:
def index
#chef = Chef.paginate(page: params[:page])
#meals = #chef.meals
end
Or just in your view:
<div class = 'span4'>
<div class = 'chef-box'>
<h2><center><%= link_to chef.name, chef %></center></h2>
<h2><center>"<%= chef.description %>"</center></h2>
<h2>Has <%= pluralize(chef.meals.size, 'meal') %></h2>
<% chef.meals.each do |meal| %>
<% meal.name %>
<% end %>
</div>
</div>
Of note – since Chef is a model name, you should be able to render your _chef.html.erb more cleanly with simply:
<div class = 'chef'>
<div class = 'row'>
<%= render #chef %>
</div>
</div>
<%= will_paginate %>
Note that #chef is singular to match your controller, you could also pass it a collection <%= render #chefs %> if that's what's set up in your controller, and it will do render each chef separately.
I am trying to submit info from a form in my view, that passes the submitted info :hashtag into the model and then runs the model with that info. But it seems thats my model just runs with the words "hashtag" instead of the form info. I believe it is close. I just can't figure out where to go next.
home.html.erb
<div class="row">
<div class="span6 offset2">
<%= form_for :hashtag do |f| %>
<div class="input-prepend input-append">
<span class="add-on swag">#</span>
<%= f.text_field :hashtag , class: "span3 inverse", id:"appendedPrependedInput" %>
<%= f.submit "Swag!", class: "btn btn-inverse" %>
<% end %>
</div>
</div>
<div class="span4">
<div id="hashtags">
<% #random_hashtags.each do |hashtag| %>
<blockquote><%= hashtag.content %></blockquote>
<div class="from">— #<%= hashtag.screen_name %></div>
<% end %>
</div>
</div>
</div>
hashtag.rb
class Hashtag < ActiveRecord::Base
attr_accessible :content, :profile_image, :screen_name, :tweet_date, :tweet_id
def self.pull_hashtag
Twitter.search("%#{hashtag}").results.map do |tweet|
unless exists?(tweet_id: tweet.id)
create!(
tweet_id: tweet.id,
content: tweet.text,
profile_image: tweet.profile_image_url,
screen_name: tweet.from_user,
tweet_date: tweet.created_at
)
end
end
end
end
hashtags_controller
class HashtagsController < ApplicationController
def home
#random_hashtags = Hashtag.order("RANDOM()").limit(4)
end
def create
#hashtag = Hashtag.pull_hashtag(params[:search])
end
end
Updated code that I am currently using now as I was not posting anything to the model
It is going though on submit but it seems nothing from there.
Update 2,
I am trying to post the information to the database, by taking the info from the form, running a Twitter.search on it and creating the results in my database.
Can you try to replace with this?
form_for #hashtag, :url => :action => 'home'
my guess is that the action needs to be specified.