Rails selector which has options a set of numbers - ruby-on-rails

I want to make a form in which the user can select a number between 1 and 30. I am trying to do something like this:
<%= f.select("#currency.neural_network", "prediction_days", [1..30]) %>
However, I am getting the below error.
Failure/Error: click_link 'Show'
ActionView::Template::Error:
undefined method `merge' for [1..30]:Array
The code for the entire form is:
<%= form_for #currency.neural_network do |f| %>
<%= f.label "Days" %><br />
<%= f.select("#currency.neural_network", "prediction_days", [1..30]) %>
<%= f.submit "Predict", class: "btn btn-primary" %>
<% end %>

What about :
<%= f.number_field(:days, in: 1..30, step: 1) %>
# => <input id="currency_days" max="30" min="1" name="currency[days]" step="1" type="number">
It basically returns you an html input tag of type "number" from 1 to 30 incremented 1 by 1. Is it what you are lookin for ?

Take a look at the documentation for select. Assuming you want prediction_days to be passed to your controller as a value between 1 and 30, and prediction_days is an attribute of a neural_network object, then the following should work for you:
<%= f.select(:prediction_days, options_for_select(1..30)) %>

Related

Ruby on Rails, method undefined

I'm very new to ruby on rails. I'm trying to make a text field to assign one of my variables (end_date), but I keep getting this error:
undefined method `end_date' for #<Quiz:0x007fccd1e0f9c0>
Here's my code:
<%# Main Canvas where cardes places %>
<div class="column large-11" id="main">
<%= form_for #quiz do |q| %>
<%= q.label :quiz_name %>
<%= q.text_field :quiz_name %>
<%= q.label :end_date %>
<%= q.text_field :end_date %>
<%= hidden_field_tag 'selected', 'none' %>
<%= q.hidden_field :classroom_id, value: #classroom_id%>
<%= q.submit "Create Quiz", class: "expanded button" %>
<% end %>
<%= form_tag("/quiz/#{#classroom_id}/copy", method: "get") do %>
<%= label :id, "ID" %>
<%= text_field_tag "id", "" %>
<%= submit_tag "Copy Quiz By ID", class: "expanded button" %>
<% end %>
</div>
Let me break down how these different pieces relate to one-another, which hopefully will make this easier for you to troubleshoot.
<%= form_for #quiz do |q| %>
Here you are invoking form_for to create a form bound to the #quiz object. It yields a form builder object as the argument q.
<%= q.text_field :quiz_name %>
Here you are calling the text_field method on the form builder with the field named quiz_name. This means it will generate a text input, and call the quiz_name method on #quiz to find the current value.
So given that background, it should be clear why you are seeing this error:
<%= q.text_field :end_date %>
You are telling the form builder to call #quiz.end_date for the value of this field, but that method does not exist.
You have not given enough code samples for us to determine why you expect this method to exist. Perhaps this is a field you've added to the quizzes table, but haven't yet run the migration? Is this supposed to be a virtual attribute on Quiz? Or perhaps you just want to send a field that isn't connected to the Quiz model inside this form. (You can do that with a separate set of helpers, in this case text_field_tag, that give you more flexibility in where the data comes from).

Undefined method when trying to pass through id

I'm trying to pass a project ID from its show page to an employee_projects form where I can display the projects name. The error I'm getting is
undefined local variable or method `project_id' for #<EmployeeProjectsController:0xb8bdd58>.
employee_project controller:
# GET /employee_projects/new
def new
puts params[project_id]
#project = Project.find(params[:project_id])
session[:project_id] = #project.id
#employee_project = EmployeeProject.new
end
employee_projects form
Assign worker for this project <%= project.projectName%><br>
<div class="field">
<%= f.label :employee_id %><br>
<%= f.collection_select :employee_id, Employee.all, :id, :empLastName, :prompt => "Select worker" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Projects show
<%= button_to "Assign!", new_employee_project_path(project_id: #project.id) , class: "btn btn-primary", :method => :get %>
Does anyone have any ideas on how to fix this?
In your controller try changing puts params[project_id] to puts params[:project_id].
params is a Hash so you must refer to its keys with symbols, that is :project_id. project_id is just a variable that has no value assigned to it, hence the error undefined local variable or method 'project_id' for #.
UPDATE ("Couldn't find Project with 'id'=" fix)
The button in projects' show view is not sending the parameter project_id even though you are passing it to the new_employee_project_path helper (check your url string and you will see it ending at ? with no parameters). To fix it, i recommend using a regular form instead of button_to helper.
So, instead of this line:
<%= button_to "Assign!", new_employee_project_path(project_id: #project.id) , class: "btn btn-primary", :method => :get %>
Try using this form:
<form action="<%= new_employee_project_path %>" method="get">
<input type="hidden" name="project_id" value="<%= #project.id %>">
<input type="submit" value="Assign!" class="btn btn-primary">
</form>

Set both end_ and start_time in form with datetime_select

Im trying to set up a bookings form where people can book services. Within the Service model there is a duration set and I want to use this to automatically populate the end_time. For example, if I book a 60 minute service at 2017-03-22 1PM I want the end time to be set at the same day but one hour later. Is it possible to send this in with a form?
<%= form_for([service, service.bookings.new]) do |f| %>
<div class="row">
<div class="col-sm-12">
<%= hidden_field_tag "recipients", #user.id %>
<div class="form-group">
<%= label_tag 'Available times' %>
<%= f.datetime_select :start %>
<%= f.hidden_field :end, value: Time.at([:start].to_i + service.duration) %>
<%= f.hidden_field :service_id, value: service.id %>
<%= f.hidden_field :price, value: service.price %>
</div>
<%= submit_tag 'Book', class: 'btn btn-complete btn-lg btn-large btn-block' %>
</div>
</div>
<% end %>
I've tried with above but get
undefined method `to_i' for [:start]:Array
which I guess is because :start is not yet saved and renders nil.
Any ideas on how to solve this?
Thanks a lot
[:start] is just return an Array contain symbol, and you can not get anything.
You should create end_time in controller or model when you got the start_time, or other way is using the javascript code get end_time when type start_time in the form
Consider not doing business logic in a view. Send these params and set the end variable in model or controller.
Why does your code not work?
datetime_select returns a set of select tags (one for year, month, day, hour, and minute).

Ruby on Rails : Advance search, undefined method [ ] for nil:Nil class

I am very new to rails and try to make an advance search form which takes two values 'blood_group' and 'area' and based upon that search the records from the database are fetched and will display on the same page (find.html.erb)
I have tried something in find.html.erb but there is an error occurred 'undefined method [] for nil:Nil class' where my search form exists. please help to get out of this error.
Here is my search form in find.html.erb
<%= form_tag find_path , method: :get do %>
<p> <%= label_tag :blood_group %><br />
<%= select_tag (:blood_group), options_for_select(%w[ A+ B+ O+ AB+ A- B- O- AB-]), params[:blood_group] %> </p>
<p> <%= label_tag :area %><br />
<%= select_tag (:area), options_for_select(%w[Indore Vijay_Nagar Bhawar_Kuwa Rajendra_Nagar Geeta_Bhawan Aerodram Tejaji_Nagar Raj_Mohalla Rajwada Chandan_Nagar Gandhi_Nagar Arvindo MY Bombay_Hospital]) , params[:area] %> </p>
<%= submit_tag "Search" , class: "btn btn-primary" , name: nil %>
<% end %>
The error is at the lines where I used select tag.
below is my find action in Donor controller
def find
#donors = Donor.search(params[:blood_group], params[:area]).all
end
And Donor.rb is as follows
class Donor < ActiveRecord::Base
def self.search(blood_group, area)
return all unless blood_group.present? || area.present?
where(['blood_group LIKE ? AND area LIKE ?', "%#{blood_group}%", "%#{area}%"])
end
end
Your search form must be like this:
<%= form_tag find_path , method: :get, remote: true do %>
<p> <%= label_tag :blood_group %><br />
<%= select_tag (:blood_group), options_for_select(%w[ A+ B+ O+ AB+ A- B- O- AB-]) %> </p>
<p> <%= label_tag :area %><br />
<%= select_tag (:area), options_for_select(%w[Indore Vijay_Nagar Bhawar_Kuwa Rajendra_Nagar Geeta_Bhawan Aerodram Tejaji_Nagar Raj_Mohalla Rajwada Chandan_Nagar Gandhi_Nagar Arvindo MY Bombay_Hospital]) %> </p>
<%= submit_tag "Search" , class: "btn btn-primary" , name: nil %>
<% end %>
Modify your controller as:
def find
#donors = Donor.search(params[:blood_group], params[:area]).all
respond_to do |format|
format.js
end
end
Then add find.js.erb to show the searched content.

Is there a way to set a distinct ID and name for a text_field in Rails 2?

I've got a Rails 2 site I'm trying to add a form handler to, but I'm running into problems converting the html form fields into form handler fields.
The form code begins with:
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
I keep getting errors when I try things like
<%= text_field :newsavedmap, :html=>{ :value => 'New Map', :name=>'newsavedmapname', :id=> 'savedmap_name', :size => '30' } %>
Error:
ActionView::TemplateError (wrong number of arguments (1 for 2)) on line #284 of app/views/layouts/maptry.html.erb:
Here are the fields. How can I convert these to form handler fields in Rails 2?
<input id="savemap_name" name="newsavedmapname" size="30" type="text" value="New Map"></p>
<select id="startdrop" name="startthere">
<OPTIONS HERE>
</select>
<select multiple id="waypoints" class="mobile-waypoints-remove" name="waypointsselected[]">
<OPTIONS HERE>
</select>
Thanks for any help you can provide!
Edit 1 Error Code for the Text_Field
Using Bigxiang's approach, I get
Processing NewsavedmapsController#create (for IP at Date Time) [POST]
Parameters: {"endhere"=>"", "endthere"=>"SAMPLE ADDRESS 1", "newsavedmap"=>{"newsavedmapname"=>"test Map"}, "startthere"=>"SAMPLE ADDRESS 2", "starthere"=>"", "optimize"=>"on"}
ActiveRecord::UnknownAttributeError (unknown attribute: newsavedmapname)
The line with "newsavedmap"=>{"newsavedmapname"=>"test Map"} should just read
"newsavedmapname"=>"test Map"
How can I do this? My controller starts with:
def create
#newsavedmap = Newsavedmap.new(params[:newsavedmap])
#newsavedmap.name = params[:newsavedmapname]
try this:
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.text_field :newsavedmapname :id=>"savemap_name", :size=>30, :value=>"New Map"%>
<%= f.select :startthere, YOUR_COLLECTIONS, {}, {:id=>"startdrop"}%>
<%= f.select :waypointsselected, YOUR_COLLECTIONS, {}, {:id=>"waypoints", :class=>"mobile-waypoints-remove", :multiple => true}%>
<% end %>
make sure YOUR_COLLECTIONS should be an array like ['a', 'b', 'c'] or [['name1', id1],['name2', id2],['name3', id3]].
If you persist the parameter is "newsavedmapname"=>"test Map", try this:
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= text_field_tag :newsavedmapname, "New Map", :id=>"savemap_name", :size=>30%>
<%= select_tag :startthere, options_for_select(YOUR_COLLECTIONS), {:id=>"startdrop"}%>
<%= select_tag :waypointsselected, options_for_select(YOUR_COLLECTIONS), {:id=>"waypoints", :class=>"mobile-waypoints-remove", :multiple => true}%>
<% end %>
But I don't understand why not use parameter's name as same as the column's name. For example, I see your newsavedmap model has a column named "name". you can use it directly
<% form_for #newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.text_field :name , :value=>"New Map" %>
<% end %>
in your controller , you can delete line #newsavedmap.name = params[:newsavedmapname]
def create
#newsavedmap = Newsavedmap.new(params[:newsavedmap])
if #newsavedmap.save
#######
end
end

Resources