Rails: How to store values dynamically or as cookie - cookies

May be the question will sound weird. But as I am doing it very first time, don't know what is it exactly called.
I have a search method with typeahead used in it on homepage of www.mealnut.com (do check a live demo). There are two entries, city and category. City shows data in typeahead array and on submit, gives the search result. Here is a code:
<%= form_tag search_path, :method => 'get' do %>
<%= text_field_tag 'search', nil, :placeholder =>"Enter your city ", :autocomplete => :off, :'data-provide' => "typeahead", :id=>"citysearch" %>
<%= select_tag :category, options_from_collection_for_select(#categories, "id", "category", params[:category]), html_options = { :prompt => 'Select a category'} %>
<%= submit_tag "Search"%>
<% end %>
And here is typeahead script:
$(document).ready(function(){
var cities = ["Bengaluru", "Pune", "New Delhi", "Delhi", "Mumbai"
$("input[data-provide='typeahead']").typeahead({source: cities});
});
And here is issue: when user clicks on back button to return on home page, city gets wiped out and category remains as selected. Here user should see city, he entered last time instead of blank field.
I think this happens cause typeahead just gives array and doesn't store value anywhere. I dont want to create separate table for array.
i found cookie might be the option. But dont know how to go with it as never tried cookie. Also, what is the best solution on this cookie or anything else? Has anybody done this?
Please help!
Update:
I used cookie for one city in controller. My home controller's index action is:
def index
#categories = Category.all
cookies[:search] = "Mumbai"
end
And modified index page form for search to:
<%= text_field_tag 'search', nil, :placeholder =>"#{cookies[:search]} ", :autocomplete => :off, :'data-provide' => "typeahead", :id=>"citysearch" %>
This shows city Mumbai on back button click. But i dont want city to be fixed. It should be user entered value. Can anyone tell how to do it?

Related

Must provide source or customer

getting an error with stripe in local testing transfer recipients.
I check SQL lite browser and the user has a stripe recipient ID
I tried clearing my entire database, creating a new listing and then placing an order and getting the same error. not sure of the cause of this error, and help would be great!
Stripe::InvalidRequestError in OrdersController#create
Must provide source or customer.
begin
charge = Stripe::Charge.create(
:amount => (#listing.price * 100).floor,
:currency => "usd",
:card => token
This was a turbo-links issue. I fixed the issue by inputting the following code
<% if user_signed_in? %>
<%= link_to 'New Listing', new_listing_path, class: "btn btn-link", data: { no_turbolink: true } %>
<% end %>

Rails 4: Dynamically change the Time method in a controller action

I am trying to drill down some searches in my app using time intervals....posts in the last day, last week, last month. I am trying to be DRY and write a search controller method that will handle this for me. I am having probelms passing in the time variable. See the form below. #search_term is the search term and hidden becasue for this form as an example, all the client sees is a link saying "last 24 hours"
<%= form_tag time_searches_path do %>
<%= hidden_field_tag :search_task, #search_term %>
<%= hidden_field_tag :time, "24" %>
<%= hidden_field_tag :Klass, Micropost %>
<%= submit_tag "Last 24 hours", class: 'Criteria'%>
<% end %>
Then I have a helper method that is included in my search controller.....
def get_time_interval time
if time == "24"
#time_variable = 1.day.ago
elsif time == "week"
#time_variable = 1.week.ago
elsif time == "month"
#time_variable = 1.month.ago
end
end
Then I add the time_variable into my controller action
def search
model = params[:Klass]
klass = model.constantize
time = params[:time]
get_time_interval time
#search = Sunspot.search [klass] do
fulltext params[:search_task]
with(:created_at).greater_than(#time_variable)
end
end
Clearly this is wrong. I assuming i need some form of metaprogramming. I am new to rails to don't really know how to proceed.
Error I get is
undefined method `gsub' for nil:nilclass
And it highlights the line with the time variable in the controller

Rails object method returning full array

I'm having an issue with an association. I've got an Employee model that belongs_to a Role model. When I try to display the association, I get the full array displayed back.
Here's the show action from my Employee controller. As you can see, I've tried a few different methods to make the proper association in the first place:
def show
#employee = Employee.find(params[:id])
# #role = Role.where(:id => #employee)
# #role = Role.find_by_sql("select roles.role_title from roles where roles.id in (select role_id from employees where role_id='1')")
#role = Role.where(id: #employee)
end
And here's the view:
<p>
<strong>Role:</strong>
<%= #role.each do |r|
r.role_title
end %>
</p>
My output comes back as:
Role: [#<Role id: 3, role_title: "Support Engineer", created_at: "2014-08-20 16:09:22", updated_at: "2014-08-20 16:09:22">]
What am I missing here?
You need to actually iterate and display something for each role.
<%= %> means "display the result of the expression", which in your case, is an each.
each returns the collection you were iterating over. You want something closer to:
<% #role.each do |r| %>
<%= r.role_title %><br/>
<% end %>
Although it obviously depends on what you actually want to display, for example:
<%= #role.collect(&:role_title).join(', ') %>
Unrelated: I might argue that Role#role_title is redundant and Role#title would be sufficient.
If the employee belongs_to a role there is only one role for each employee.
You can retrieve it as easily as specifying...
#employee.role
but if you insist on constructing a separate retrieval then
#role = Role.where(id: #employee.role_id).first
EDIT
So talking about the views... if there's only one #role you don't need to iterate through an array...
<p>
<strong>Role:</strong> <%= #role.role_title %>
</p>
You're seeing an array because the where returns an array, you could bypass that with...
#role = Role.where(id: #employee).first
As Dave Newton pointed out, if it really was an array you'd need to do...
<p>
<strong>Role:</strong>
<% #role.each do |r| %>
<%= r.role_title %>
<% end %>
</p>

Rails 4 update attribute (belongs_to) with click on link ( within dropdown list on show page)

In my app I have Issue model, which belongs_to Status model. What I need to do is to have dropdown list of links (or spans, doesn't matter) with Statuses Id's on Issue show page, so I could change status clicking on this (could be non-ajax and ajax).
I am rather new to Rails, so I do not know how to implement this, was thinking of few ways to do this, but non seems to be working.
Because you're new, I'll outline what I'd do for you (hopefully it will help):
#config/routes.rb
resources :issues do
get "change_status/:status_id", to: "issues#change_status"
end
#app/models/status.rb
Class Status < ActiveRecord::Base
has_many :issues
scope :not, ->(id) { where('statuses.id != ?', id) }
end
#app/controllers/issues_controller.rb
def show
#issue = Issue.find params[:id]
#statuses = Status.all.not(#issue.id)
end
def change_status
issue = Issue.find params[:id]
issue.status = params[:status_id]
issue.save if issue.status_id_changed?
redirect_to issue
end
#app/views/issues/show.html.erb
<%= #statuses.each do |status| %>
<%= link_to status.title, issue_change_status_path(#issue, status) %>
<% end %>
There's obviously some stuff to explain - if you need me to do that, let me know and I'll give you the details!
You can simply use the link_to rails helper like in the following code:
link_to "Change Status", my_path_to_update_status, remote: true, method: :put
This will send an AJAX PUT request (thank to the remote: true) to your my_path_to_update_status.
Then, you just have to update the status in the action corresponding to the path specified in the link_to helper tag.
More information about the link_to rails helper here

error getting path returned rails 4

I'm trying to access my page gallery/new via a link, so i have created this
<%= link_to 'New gallery' new_gallery_path %>
rake routes gives
gallery_index_path GET /gallery(.:format) gallery#index
POST /gallery(.:format) gallery#create
new_gallery_path GET /gallery/new(.:format) gallery#new
edit_gallery_path GET /gallery/:id/edit(.:format) gallery#edit
gallery_path GET /gallery/:id(.:format) gallery#show
PATCH /gallery/:id(.:format) gallery#update
PUT /gallery/:id(.:format) gallery#update
DELETE /gallery/:id(.:format) gallery#destroy
and within my routes i have
resources :gallery
My view at gallery/new is
<%= nested_form_for #gallery, :html => { :multipart => true} do |f| %>
--content here
<% end %>
whenever i click the link to view this page i get
undefined method `galleries_path
Can someone point out my mistake, please?
You've chosen the wrong name for your resources. It should always be pluralized:
resources :galleries
From this Rails will generate the plural and singular paths correctly. galleries_path for the index, gallery_path for show, etc etc.