Newbie on RoR. I want to search division, category and brand (by name, not by id). I have a problem to figure out a search function. Hopefully I can get out from my problem. What should I do to convert (division_id) --> (division_name), etc?
**#Model**
item.rb :
belongs_to :division
belongs_to :category
belongs_to :brand
def self.search(params)
items = Item.all
items = items.where("division_id BETWEEN #{params[:division_from].to_i} AND #{params[:division_to].to_i}") if params[:division_from].present?
items = items.where("category_id BETWEEN #{params[:category_from].to_i} AND #{params[:category_to].to_i}") if params[:category_from].present?
items = items.where("brand_id BETWEEN #{params[:brand_from].to_i} AND #{params[:brand_to].to_i}") if params[:brand_from].present?
end
category.rb :
has_many = items
division.rb :
has_many = items
brand.rb :
has_many = items
**#View**
items- index.html.erb
<fieldset><legend>Filter</legend></fieldset>
<div class="panel-body">
<div class="row">
<%= form_tag search_items_path, class: "form-horizontal bucket-form", remote: true do %>
<div class="col-md-6">
<div class="form-group">
<div class="text-field">
<div class="col-md-10">
<td>Division</td>
<div class="input-group input-large">
<%= text_field_tag 'division_from','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'division_to','', class: "form-control dpd1" %>
</div>
<td>Category</td>
<div class="input-group input-large">
<%= text_field_tag 'category_from','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'category_to','', class: "form-control dpd1" %>
</div>
<td>Brand</td>
<div class="input-group input-large">
<%= text_field_tag 'brand_from','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'brand_to','', class: "form-control dpd1" %>
</div>
<div class="btn-group">
<%= submit_tag 'Search', class: "btn btn-primary" %>
</div>
</div>
</div>
</div>
</div>
<% end %>
</div>
</div>
**#Controller**
items_controller.rb :
def search
#items = Item.search(params)
respond_to do |format|
format.js
end
end
If I execute my code, I have search by id (not by name).. Thanks before
Well.. first, if you want to search only one division-category-brand, change the view for every item only one text input (but its better to use select/multiselect).
Using text input:
only one text input per class
e.g. <%= text_field_tag 'brand','', class: "form-control dpd1" %>
in the item.rb change the search method to this
def self.search(params)
items = Item.all
items = items.where(division_id:Division.find_by_name(params[:division])) if params[:division].present?
items = items.where(category_id:Category.find_by_name(params[:category])) if params[:category].present? items = items.where(brand_id:Brand.find_by_name(params[:brand])) if params[:division].present?
end
It will not work when you're not filling the correct name in the view, so i suggest to use select/multiselect.
I hope it will work.
Related
Ok so Ive looked around at many answers posted on stackoverflow and many other sites because I know that this is a commonly asked question but I just cant seem to get it and would really appreciate it if someone could point me in the right direction. What Im trying to achieve here is really simple, Im trying to render a new form from another model, Post into my homepage, which is another controller that I created. So basically in my homepage it shows a list of all the recent Post that were created by users. Furthermore I wanted to user to be able to directly add a new Post from the homepage. Right now I have it displaying however clicking on the submit button does absolutely nothing.
Here is the form for POST
<div class="panel panel-primary">
<div class="container">
<div class="panel-heading">
<%= image_tag #post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= #post.user.name %>
<%= form_for(#post) do |f| %>
<% if #post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :content %><br>
<%= f.text_area :content %>
</div>
<div class="field">
<%= f.file_field :avatar %>
</div>
</div>
</div>
<div class="panel-footer">
<div class="actions">
<%= f.submit %>
</div>
</div>
<% end %>
</div>
Here is my homepage controller
class HomeController < ApplicationController
def index
if user_signed_in?
#posts = Post.all
#post = current_user.posts.build
else
redirect_to new_user_registration_path
end
end
def create
#post = current_user.posts.build(post_params)
#post.save
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
#post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:user_id, :content, :avatar)
end
end
Lastly my homepage index
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-5">
<h1>Home#index</h1>
<p>Find me in app/views/home/index.html.erb</p>
<%= render 'posts/form' %>
<% #posts.each do |post|%>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
<%= image_tag post.user.avatar.url(:post_pic), class:"img-thumbnail" %>
<%= post.user.name %>
</h3>
</div>
<div class="panel-body">
<%= post.content %></br></br>
<% if post.avatar.file? %>
<%= image_tag post.avatar.url(:test), class:"img-thumbnail" %></br>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
Routes
Rails.application.routes.draw do
resources :posts
get 'users/index'
get 'users/show'
root 'home#index'
devise_for :users
So, you're doing like saving post data using another Controller(Home) instead of Post Controller, right?
Your partial form for post uses:
<%= form_for(#post) do |f| %>
// #post generated url (/posts)
// will look for create method in POST Controller
I suggest to put your saving codes in proper controller(posts), so you will prevent code duplication/redundancy.
TIP:
If you are worrying about redirection from Home view or from Posts view,
you can use params[:controller] for checking where the request came from, by just using if statement.
Finally, you can redirect it to proper page after saving.
I'm still learning in RoR and I have a problem to search table column (period) by date range (start_period, end_period). This is my code :
promotions_controller
def search
#promotions = Promotion.search(params)
respond_to do |format|
format.js
end
end
index.html.erb - promotions
<%= form_tag search_promotions_path, class: "form-horizontal bucket-form", remote: true do %>
<div class="col-md-6">
<div class="form-group">
<td>Periode</td>
<div class="input-group input-large">
<%= text_field_tag 'start_period','', class: "form-control dpd1" %>
<span class="input-group-addon">To</span>
<%= text_field_tag 'end_period','', class: "form-control dpd1" %>
</div>
</div>
</div>
<% end %>
promotion.rb
def self.search(params)
promotions = Promotion.all
????
end
I have tried to create a code for (start_period) & (end_period) in my model. But I have failed :( Can you give me a solution for my problem? Thanks
Try the following method:
def self.search(start_period, end_period)
Promotion.where("start_period >= ? AND end_period <= ?", start_period, end_period)
end
If you place these records on your seeds file (rake db:seed):
Promotion.create!([
{name: 'p1', start_period: DateTime.new(2001), end_period: DateTime.new(2002)},
{name: 'p2', start_period: DateTime.new(2001), end_period: DateTime.new(2003)},
{name: 'p3', start_period: DateTime.new(2008), end_period: DateTime.new(2009)}
])
And execute
Promotion.search(DateTime.new(1999), DateTime.new(2004)) # returns p1 & p2
I am using devise and simple form with rails. I can't figure out why a field with errors div or any div for that matter is not being created when there is a failed validation. I have tried deleting everything after f.input :first_name and still no new divs. Let me know what other info you might need to help me figure out my problem.
<% provide(:title, 'Sign up') %>
<h1>Sign up</h1>
<div id="sign-up" class="row">
<div class="col-md-6 col-md-offset-3">
<%= render 'shared/error_messages' %>
<%= simple_form_for(resource, :as => resource_name,
:url => registration_path(resource_name),
html: {class: "well"}) do |f| %>
<ul class="inline-layout">
<li>
<%= f.input :first_name, required: false,
placeholder: 'First name',
label: false,
error: false,
input_html: {class: 'form-control inherit-width'} %>
</li>
Here is the error_messages partial:
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-danger">
The form contains <%= pluralize(#user.errors.count, "error") %>.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
Edit:
Looks like I am getting a new div class 'error' but I want it to be 'field_with_errors' I tried changing all instances of 'error' in the simple_form.rb file but it didn't change anything. I'll just have to change my css.
In my Rails app, the labels for the fields on the login page are way too close to the fields themselves, making it look cramped. I want to add space between them, but am not sure how.
I have Rails 4 with simple_form, bootstrap 3, and devise installed.
This is my app/views/devise/sessions/new.html.erb code:
<div class="row">
<div class="col-xs-4">
<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: "well"}) do |f| %>
<fieldset>
<legend>Log in</legend>
<%= f.input :email %>
<%= f.input :password %>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.input :remember_me, as: :boolean %>
</div>
</fieldset>
<% end -%>
<div class="actions">
<%= f.button :submit, "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
<div class="col-xs-8">
<h2>Signing in is easy and secure</h2>
</div>
</div>
And here is the entire github repo: https://github.com/yamilethmedina/wheels_registration
I've been looking in my bootstrap.css file and can't find relevant classes. What do you think I should do next?
You could add your own label with space after the custom label.
<%= f.input :email, label: 'Email ' %>
<%= f.input :password, label: 'Password ' %>
Or you could use tools like chrome dev tools to inspect the label element to see what class the labels are given and then add some custom css. example .label-class { margin-right: 10px; }
I've been looking all over the web for an answer to this for the past three hours so I'm going to post a question. I'm trying to create some basic forum software but I am having trouble with my relationships. This is the error I get
undefined method `forums' for # <ActiveRecord::Relation::ActiveRecord_Relation_Category:0x3706cb0>
my categories/index file
<% for category in #category %>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><%= link_to category.name, category_path(category.id) %></h3>
<h4><%= category.description %> </h4>
</div>
<% for forum in #category.forums %>
<div class="panel-body">
<h4><span class="glyphicon glyphicon-tree-conifer"></span><%= link_to forum.name, forum_path(forum.id) %> </h4>
</div>
<hr />
<% end %>
</div>
<% end %>
Categories Controller
class CategoriesController < ApplicationController
def index
#category = Category.all
end
def show
#category = Category.find(params[:id])
end
end
forums controller
class ForumsController < ApplicationController
def show
#forum = Forum.find(params[:id])
end
end
category model
class Category < ActiveRecord::Base
has_many :forums, dependent: :destroy
end
forum model
class Forum < ActiveRecord::Base
belongs_to :category
has_many :threads, dependent: :destroy
end
and my routes
ForumName::Application.routes.draw do
root 'static_pages#home'
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :categories, :path => "forum"
resources :forums
resources :topics
resources :posts
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
end
Hopefully I've provided enough info if anything else is required I'll update the post. Usually I can work these things out using google but this really has me stumped.Thanks in advance.
Issue in action CategoriesController#index
Should be changed to
#categories = Category.all
because Category.all expects to get several categories, not one
Also you should change categories/index file
<% for category in #categories %>
<div class="panel panel-success">
<div class="panel-heading">
<h3 class="panel-title"><%= link_to category.name, category_path(category.id) %></h3>
<h4><%= category.description %> </h4>
</div>
<% for forum in category.forums %>
<div class="panel-body">
<h4><span class="glyphicon glyphicon-tree-conifer"></span><%= link_to forum.name, forum_path(forum.id) %> </h4>
</div>
<hr />
<% end %>
</div>
<% end %>