Rails 4 Search By Category - ruby-on-rails-4

I want to put a search form on my homepage at pages#home. I'd like to be able to search my model by category. I haven't been able to find a solution where you're actually putting the logic on a different controller than the models you're searching on. I was wondering if someone could help with the syntax and where it needs to go. Here are my relations:
vendor.rb
class Vendor < ActiveRecord::Base
validates :category_id, presence: true
belongs_to :category
end
category.rb
class Category < ActiveRecord::Base
has_one :vendor
end
pages_controller.rb
def home
#vendors = Vendor.all
end
routes.rb
root 'pages#home'
I'm trying to put the search form on home.html.erb, which is under the pages layouts. Was hoping someone could help with how I can accomplish this. This being (seemingly) a simple type of search, I'd hopefully not have to use a gem.
EDIT: ANSWER
For those searching, here's what worked. Thank you #Vla
vendors_controller.rb
def search
#vendors = Vendor.search(params)
end
pages/home.html.erb (this is my root 'pages#home')
<%= form_tag vendors_search_path, method: :get do |f| %>
<%= text_field_tag :search, params[:search], placeholder: "Search Vendors" %>
<%= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}) %>
<%= submit_tag 'Search' %>
<% end %>
routes.rb (make sure to put this near the top)
get 'vendors/search'
vendors/search.html.erb
<% #vendors.each do |vendor| %>
<%= vendor.name %>
<%= vendor.phone %>
<%= vendor.email %>
<% end %>

Ok, You can do it this way:
Go to Your vendor.rb file and add search method like this:
def self.search params
vendors = Vendor.where(category_id: params[:category].to_i) unless params[:category].blank?
vendors
end
Then in Your vendors_controller create search method:
def search
#vendors = Vendor.search(params)
end
Then create form at homepage similar to:
= form_tag search_vendors_path, method: :get, role: 'form' do
.form-group
%label Category
= select_tag 'category', options_for_select(Category.all.map{|el| [el.name, el.id]}), class: 'form-control'
= submit_tag 'Search'
And do not forget to put
get 'vendors/search'
into Your routes and add "search" view.
You can still do the same without search action, with result on Your homepage. Anyway I hope You got the idea.

Related

Nested attributes showing when editing but not when creating a new record in rails

When I edit a model with nested attributes everything works perfectly. If I try to create a new record with nested attributes, the nested attributes don't appear on the form that has the nested attributes. I have two models as follows
class JobSpec < ActiveRecord::Base
has_many :availabilities
accepts_nested_attributes_for :availabilities, allow_destroy: true
end
and
class Availability < ActiveRecord::Base
belongs_to :job_spec
end
in the job_specs_controller I have
class JobSpecsController < ApplicationController
def new
#job_spec = JobSpec.new
#availabilities = Availability.none
availability = Availability.new
availability.job_spec = #job_spec
#availabilities << availability
end
def edit
#job_spec = JobSpec.find(params[:id])
#availabilities = #job_spec.availabilities
end
def job_spec_params
params.require(:job_spec).permit(
... all of the job_spec attributes,
availabilities_attributes: [:id, ... all of the availability attributes],
)
end
end
In _form for job_spec I have
<% #availabilities.each do |availability| %>
<%= f.fields_for availability do |builder| %>
<%= render partial: 'availabilities/form_mini',
locals: {f: builder, availability: availability} %>
<% end %>
<% end %>
Since editing works, I assume there's not much wrong. Any ideas?
Update
I've almost sorted out all the problems but still have one. Here's what I've done.
1) I had to build the job_spec availabilities. The new action of the JobSpecsController now looks like this
def new
#job_spec = JobSpec.new
#job_spec.client_id = params[:client_id]
#job_spec.availabilities.build
end
2) I wanted to be able the destroy the nested models, so I had to pass :_destroy in with the nested attributes to job_spec_params. That method looks like this
def job_spec_params
params.require(:job_spec).permit(
... all of the job_spec attributes,
availabilities_attributes: [:id, :_destroy, ... all of the availability attributes],
)
end
end
3) Finally, I had to change _form for job_spec. I have
<%= f.fields_for :availabilities do |builder| %>
<%= render partial: 'availabilities/form_mini', locals: {f: builder} %>
<% end %>
The only issue I have now is that I want to pass and instance variable through to my availability partials. Something like this
<%= render partial: 'availabilities/form_mini', locals: {f: builder, a: availability} %>
How should that be done? I'm not sure how to iterate over all the #availabilities while rendering all the partials.
Glad you worked some of the issues out. First one small improvement
#job_specs_controller.rb
def new
#job_spec = JobSpec.new(client_id: params[:client_id])
#job_spec.availabilities.build
end
And to overcome your last issue, the solution is a mix of what you had before and after. You can pass the actual object in the fields_for method. It's like that:
#_form.html.erb
<% #job_spec.availabilities.each do |availability| %>
<%= f.fields_for :availabilities, availability do |builder| %>
<%= render partial: 'availabilities/form_mini',
locals: {f: builder, a: availability} %>
<% end %>
<% end %>

trying to share a model in my app

I am creating a website using a rails and I would like to share the model. I followed The tutorial for creating an article. In the ArticlesController is index, show, new, edit, create, update and destroy. I figured I would use this as the data entry part of my app. Then I was going to create a PageController that has home, news, videos, music and events. Here is my set up:
here is my Article model
class Article < ActiveRecord::Base
has_attached_file :image, styles: { large:"700x700>", medium:"300x300>", thumb:"150x150#"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
validates :title, presence: true,
length: { minimum: 5 }
belongs_to :page
end
here is my page model
class Page < ActiveRecord::Base
has_many :article
end
here is my page controller
class PageController < ApplicationController
def home
#page = Page.includes(:articles)
end
def news
end
def videos
end
def music
end
def events
end
end
here is my home view
<h1>Page#home</h1>
<p>Find me in app/views/page/home.html.erb</p>
<%= #page.articles.each do |article| %>
<%= article.title %>
<%= article.content %>
<%= article.image %>
<% end %>
here is the error I get
undefined method `articles' for #<ActiveRecord::Relation []>
not clear onwhats going on. Any help would be appreciated
From the limited information you posted, I am assuming the best thing for you to do is create an association. Pages should has_many articles and articles should belong_to a page. http://guides.rubyonrails.org/association_basics.html
After that is setup, your controller can be set up as follows:
#page = Page.includes(:articles)
This will load your pages and its related articles in a single SQL query, then you can call
#page.articles.each |article|
puts article.title
puts article.content
puts article.image
end
You can now access all the information and use it as you please.

Rails collection help. Not understandng how it works

I have about 3 models that all relate to each other.
Class Invoice < ActiveRecord::Base
belongs_to :shipperline, foreign_key: 'receiver_quantity'
def shippedqty
Shipperline.getshippqty(#customerid).to_a.split
end
Then i have a shipperline model
Class Shipperline < Activerecord::Base
has_many :invoices
def self.getshippqty(customerid)
Shipperline.joins(:customerorderline, :shipper).select("cust_order_id").where(cust_order_id: customerid).order("shipped_qty").pluck(:shipped_qty).uniq
end
However in my form (which is on my main view because this query will usually return 4 to 5 results per invoice which is why i need a user to select which one it really belongs to.
So i have been trying
<%= form_for :invoice do |f| %>
<td id="putcolor"><%= f.select :shippedqty, Shipperline.getshippqty(#customerid).to_a.split %> </td>
<% end %>
but it becomes empty. I have also tried collection_select and that dose not work either.
<td id="putcolor"><%= f.collection_select :invoice_id, #shipqty, :id, :shippedqty %></td>
but then i get undefined method 'map' for nil:NilClass
Can someone explain what im doing wrong or please point me in the right direction? I am fairly new to rails.
Also is there a better approach to trying to get a select box with these various results in it for the user to pick from?
Got this to work much easier this way.
My Invoice_helper.rb
def options_for_invoices(customerid)
Invoice.shippedqty2(customerid)
end
def options_for_packlistid(customerid, shipqty)
Shipperline.packlist(customerid, shipqty)
end
Then in my form i did:
<%= form_for(#invoice) do |f| %>
<%= f.label :shippqty %>
<%= f.select :shippedqty, options_for_invoices(#invoice.customerorderid) %>
<br />
<%= f.label(:packlistid %>
<%= f.select :packlistid, options_for_packlistid(#invoice.customerorderid, #invoice.shippedqty) %>
<%= f.submit %>
<% end %>
Now when it shows a drop down collection with the correct values and inserts the correct values into the database for each record.

Rendering a form from another controller in Ruby on Rails 4

I am trying to show a form on the contact page, but it is from another controller.
The current code results in "First argument in form cannot contain nil or be empty"
After searching it looks there is a problem with the local hash that is not passed.
How can I correctly pass the locals with this code so that it works?
inquiries_controller.rb
class InquiriesController < ApplicationController
def new
#inquiry = Inquiry.new
end
def create
#inquiry = Inquiry.new(params[:inquiry])
if #inquiry.deliver
render :thank_you
else
render :new
end
end
end
inquiries_form.html.erb
<%= form_for #inquiry do |f|
f.text_field :name
f.text_field :email
f.text_area :message
f.submit "Send"
end %>
static_pages\contact.html.erb
<%= render "inquiries/form",
:inquiry => #inquiry %>
HI try adding this to your StaticPages Controller
class StaticPagesController < ApplicationController
def contact
#inquiry = Inquiry.new
end
end
Its a very common mistake. Also I believe your form may also be wrong unless you are using a gem that allows for that type of form. Let me know if this will fix your error.

saving empty records via form_for - wrong number of arguments

Update: Zishe figured it out. Correct params.require code should be:
def adventure_params
params.require(:adventure).permit(:story, :choice, :parent_id, :user_id)
end
with parenthesis instead of a bracket, of course.
Original question:
I am making a form_for that should be submitting 4 attributes to the Adventure model. The adventure model has ancestry. I keep getting wrong number of arguments (4 for 1) based on my params.require method. When I change to requirements down to 1, I see that all the attributes are blank in my database. I know they are in the params but for some reason they are not being saved. Here is my code:
Form
<div class="form">
<%= form_for #adventure do |f| %>
<%= f.label :choice %>
<%= f.text_area :choice %>
<%= f.label :story %>
<%= f.text_area :story %>
<%= f.label :parent_id %>
<%= f.text_field :parent_id %>
<%= f.submit "Post"%>
<% end %>
</div>
Controller
class AdventuresController < ApplicationController
before_filter :authenticate_user!
def home
end
def index
end
def new
#parent_id = params[:parent_id]
#adventure = Adventure.new
end
def show
#adventure = Adventure.find(params[:id])
end
def create
#user = current_user
#adventure = current_user.adventures.build(adventure_params)
if #adventure.save
flash[:success] = "Adventure created!"
redirect_to #adventure
else
flash[:error] = "There was an error"
redirect_to adventures_path
end
end
private
def adventure_params
params.require(:adventure).permit[:story, :choice, :parent_id, :user_id]
end
end
Model
class Adventure < ActiveRecord::Base
has_ancestry
belongs_to :user
validates :user_id, presence: true
validates :parent_id, presence: true
end
I have no idea why I am getting wrong number of arguments since the attributes show up in the params.
Change permit[...] to:
.permit(:story, :choice, :parent_id, :user_id)