undefined method `merge' for 35:Fixnum - ruby-on-rails

Found it
Don't use <%= f.hidden_field :field, number %> , use <%= f.hidden_field :field, value: number %>
Issue below
An ActionView::Template::Error occurred in bookings#new:
undefined method `merge' for 35:Fixnum
app/views/bookings/_form.html.erb:31:in `block in _app_views_bookings__form_html_erb__2731573742378725623_70113682151640'
Getting this horrible general error from our production website, and it's not clear why. It's not happening on our local hosts. Here's the line referenced above:
<%= current_employer.locations.first.name_or_address_1 %>
where name_or_address_1 is:
return "from #{name}" if name.present?
"from #{address_1}"
I've gone into the console and run "name_or_address_1" for every location in our database, which works fine, and "locations.first.name_or_address_1" for every employer in our database. Again, works fine. So surely it can't be this line?
Edit: I just commented out the line, deployed to production, and the error still occurs. What's going on? Why is the wrong line being referenced?
Here's the partial:
<%= form_for #employer, url: bookings_path, method: :post, html: { class: "main-form", id: "create-booking" } do |f| -%>
<% #employer.errors.full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<div id="bookings">
<ol class="numbers">
<li>
<legend>Location, Pay, & Vehicle</legend>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Type of job</label><br>
<%= f.select(:job_type_id, options_from_collection_for_select(JobType.all, :id, :name_with_delivery), {}, { id: 'job-type', class: 'form-control' }) %>
</div>
<div class="col-sm-6">
<label>Vehicle needed</label><br>
<%= f.select(:vehicle_id, options_from_collection_for_select(Vehicle.all, :id, :name), {}, { id: 'vehicle-type', class: 'form-control' }) %>
</div>
</div>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6">
<label>Location</label>
<% if current_employer.locations.size > 1 %>
<%= f.select :location_id, options_from_collection_for_select(current_employer.locations.all, :id, :name_or_address_1), {}, { class: 'form-control' } %>
<% elsif current_employer.locations.size == 1 %>
<p><strong>Location: </strong><%#= current_employer.locations.first.name_or_address_1 %></p>
<%= f.hidden_field :location_id, current_employer.locations.first.id %>
<% end %>
<%= link_to "or add new location", new_employer_location_path(current_employer, Location.new) %>
</div>
<div class="col-sm-6">
<%= f.label :pay %>
<span id="length-message" class="pull-right" style="color: #a94442"></span>
<br>
<%= f.text_field :pay, class: 'form-control', id: 'pay', maxlength: '18' %>
</div>
</div>
</div>
</li>
<legend>Shifts</legend>
<%= render 'booking', booking: Booking.new %>
</ol>
</div>
<%= link_to "Add another shift", "javascript:;", class: 'add-shift', style: 'margin-left: 40px;' %>
<script type="text/javascript">
$(".add-shift").click(function(){
$("ol.numbers").append("<%= j render 'booking', booking: Booking.new %>")
});
</script>
<%= f.submit "Post Shifts", class: 'btn green-button pull-right' %>
<br>
<br>
<br>
<span class="error-message bg-danger pull-right">
</span>
<% end %>

Don't use
<%= f.hidden_field :field, number %>
use
<%= f.hidden_field :field, value: number %>

Related

I am unable to dynamically render add a new partial form for the given association in ruby on rails. I am using cocoon gem

<%= form_for #poll do |f| %>
<%= render 'shared/errors', object: #poll %>
<div class="form-group">
<%= f.label :topic %>
<%= f.text_area :topic, rows: 3, required: true, class: 'form-control' %>
</div>
<div class="panel panel-default">
<div class="panel-heading">Options</div>
<div class="panel-body">
<%= f.fields_for :vote_options do |options_form| %>
<%= render 'vote_option_fields', f: options_form %>
<% end %>
<div class="links">
<%= link_to_add_association 'add option', f, :vote_options%>
</div>
</div>
</div>
<%= f.submit 'Create', class: 'btn btn-primary btn-lg' %>
<% end %>
link_to_add_association doesn't work. It shows that the template is missing when the template is actually present.
Here is my _vote_option_field.html.erb
<div class="nested-fields">
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', required: true %>
</div>
<%= link_to_remove_association "remove option", f %>
</div>
Maybe you can add the partial path as html_options. Something like this:
<div class="links">
<%= link_to_add_association 'add option', f, :vote_options, partial: 'your/partial/path'%>
</div>`

Rails - Kaminari - paginate_array returns duplicate records

I'm trying to paginate the following array. At the moment the table is being appended to, but only with the same records as the first page:
def collection
#collection ||= begin
viewable_pupils.where(level: level, group_id: group_id).ordered
.map{ |p| Schools::Admin::PupilReportTableFacade.new(p, school.calendar_range_for_year_starting(report_start_year)) }
end
#collection = Kaminari.paginate_array(#collection).page(params[:page]).per(10)
end
helper_method :collection
My view is as follows:
<div class="container">
<%= render 'schools/admin/reporting/collection_partial' %>
</div>
<%= link_to 'Load More', school_reporting_load_more_path, remote: true, id: "load_more_link", class: 'btn btn-info' %>
And the partial being rendered:
<% collection.each do |pupil| %>
<div class="well well--bordered well--skinny well--collapse-pad well--shadow">
<div class="marking-header marking-header--table marking-header--table-even">
<div class="marking-header__cell">
<%= pupil.first_name %>
</div>
<div class="marking-header__cell"><%= pupil.last_name %></div>
<div class="marking-header__cell" align="center">
<% pupil.histories.each do |history| %>
<% next unless school.calendar_range_for_year_starting(report_start_year).cover?(history[:created_at]) %>
<%= history[:year] %> <%= history[:level] %>
<%= history[:created_at] %>
<% end %>
</div>
<div class="marking-header__cell">
<%= "Total completed: #{pupil.pupil.activities_completed_year_starting(report_start_year)}" %></br>
<%= "(#{pupil.pupil.comprehensions_completed_year_starting(report_start_year)} x comprehensions)" unless pupil.pupil.comprehensions_completed_year_starting(report_start_year).zero? %></br>
<%= "(#{pupil.pupil.crosswords_completed_year_starting(report_start_year)} x crosswords)" unless pupil.pupil.crosswords_completed_year_starting(report_start_year).zero? %></br>
<%= "(#{pupil.pupil.debates_completed_year_starting(report_start_year)} x debates" unless pupil.pupil.debates_completed_year_starting(report_start_year).zero? %></br>
<%= "(#{pupil.pupil.word_definitions_completed_year_starting(report_start_year)} x word definitions" unless pupil.pupil.word_definitions_completed_year_starting(report_start_year).zero? %>
<div class="gems">
<% pupil.milestone_badges.each do |badge| %>
<%= image_tag image_path(badge.image), class: 'gem' %>
<% end %>
</div>
</div>
<div class="marking-header__cell">
<input type="checkbox" name="pupils[]" value="<%= pupil.id %>" class="tick-check" id="check-<%= pupil.id %>" data-behaviour="update-compare-count">
<label for="check-<%= pupil.id %>"></label>
<%= "Total this year: #{pupil.pupil.total_points_this_year}" %></br>
<%= "(Total all time: #{pupil.pupil.total_points})" %></br></br>
<%= "iHub points: #{pupil.pupil.score}" %></br>
<%= "Teacher Points: #{pupil.pupil.total_points_year_starting(report_start_year)}" %></br>
<%= "Words Read: #{pupil.pupil.words_read_year_starting(report_start_year)}" %></br>
</div>
</div>
</div>
<% end %>
Finally my ajax:
$('.container').append("<%= escape_javascript(render 'schools/admin/reporting/collection_partial')%>");
$('#load_more_link').replaceWith("<%= escape_javascript(link_to 'Load More', school_reporting_load_more_path, remote: true, id: "load_more_link", class: 'btn btn-info')%>");
If any more info is needed please ask, I feel I've tried everything I can think of at this point.

Accessing virtual attribute from params

ruby 2.1.5
rails 4.2.1
I am trying to figure out how to access a virtual attribute from params.
In my books_controller.rb, I have:
wrap_parameters :books_list
def product_params
params.require(:product).permit(:author_name, :book_name, :books_list)
end
Obviously, the books_list is the virtual attribute. I will be saving it to the books_list table
In my new.html.erb,I have:
<% #title = "Books" %>
<div class="container">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<%= form_for(#books, html: { class: 'form-horizontal' }) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.form_group :author_name do |f| %>
<%= f.label :author_name, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_field :author_name, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
<div class="form-inputs">
<%= f.form_group :book_name do |f| %>
<%= f.label :book_name, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_field :book_name, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
<%= f.form_group :books_list do |f| %>
<%= f.label :books_list, class: 'control-label col-md-2' %>
<div class='col-md-8'>
<%= f.text_area :books_list, class: 'form-control' %>
<%= f.error_messages %>
</div>
<% end %>
</div>
<div class="form-actions col-md-offset-2 col-md-10">
<%= f.submit class: 'btn btn-primary' %>
<%= link_to "Cancel", test_sets_path, class: 'btn' %>
</div>
<% end %>
</div>
</div>
</div>
When I submit the form, here's what I get for params:
Parameters: {
"utf8"=>"✓",
"authenticity_token"=>"5mxkibQHkwRnzVU31A6pe9uezsmaWeYbCUgU+gUZPLmiAZPnN6si+smdEePU0lfGITh7gmmyChf/bOY+YQQcQg==",
"books"=>{"author_name"=>"Some Author", "book_name"=>"Dwan Of The Dead"},
"books_list"=>{"{:class=>\"form-control\"}"=>"book1\r\nbook2"},
"commit"=>"Create Book List"
}
I am having trouble accessing the books_list in params. Any ideas?
If books_list column is not exist in #book, it raises error.
There is no method f.form_group in rails, I think you use gem that defines form_group method.
Maybe "books_list"=>{"{:class=>\"form-control\"}"=>"book1\r\nbook2"} parameter is made by some bootstrap gem. you check check name attribute in html source.

Rails 4 - Simple form arguments

Can anyone see what's wrong with this form of expression?
<%= simple_fields_for :article do |f| %>
<%=f.input :q, :nil, placeholder: "Search" %>
<% end %>
Error message says:
wrong number of arguments (3 for 1..2)
So- my wider code is:
articles#index.html.erb:
<div class="row">
<div class="col-md-10 col-md-offset-1">
<%= render 'articles/searchform' %>
</div>
<div class="col-md-10 col-md-offset-1">
<% Article.all.sort_by(&:created_at).in_groups_of(3) do |group| %>
</div>
<div class="row">
<% group.compact.each do |article| %>
<div class="col-md-4">
<div class="indexdisplay">
<%= image_tag article.image_url, width: '100%', height:
'200px' if article.image.present? %>
<div class="indexheading"> <%= link_to article.title, article %> </div>
<div class="indexsubtext"> <%= truncate(article.body, :ommission =>
"...", :length => 250) %></div>
</div>
</div>
<% end %>
</div>
<div class="row">
<%= link_to 'New Article', new_article_path %>
</div>
In my views articles#form.html.erb
<div class="col-xs-10 col-xs-offset-1">
<%= simple_form_for(#article) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :title, autofocus: true %>
<%= f.input :body, :label => "Post", :input_html => {:rows => 10} %>
<%= f.input :image, :label => "Add an image" %>
</div>
<div class="form-actions">
<%= f.button :submit, "Review a draft", :class =>
'formsubmit' %>
</div>
<% end %>
In articles#searchform.html.erb
<% f.simple_fields_for :article do |a| %>
<%=a.input :q, :nil, placeholder: "Search" %>
<% end %>
I get this error:
undefined local variable or method `f' for #<#<Class:0x007f874133c410>:0x007f8740448058>
You are missing your form variable, e.g. f.
For instance if you have simple_form_for ... do |f| you should then write
= f.simple_fields_for :article do |article|
= article.input :q, :nil, placeholder: 'search'
[EDIT] You seem confused. The simple_fields_for is a special command to iterate over nested items, inside a form (e.g. comments for a post). So simple_fields_for would require a surrounding form (simple_form_for). For your search-form you should just be using simple_form_for.

My submit button won't work in rails and I'm not sure why

I'm creating a simple form in rails and the submit button doesn't work. I think I'm missing something obvious here but the html seems to be fine (I'm far froma front end expert). Any advice or pointers?
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2>Apply</h2>
<hr class="star-primary">
</div>
</div>
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<div class="row control-group">
<% if #user.errors.any? %>
<div id="error_explanation">
<div class="alert alert-error">
The form contains <%= pluralize(#user.errors.count, "error") %>.
</div>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li>* <%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="col-xs-12 floating-label-form-group controls">
<%= form_for #user, url: users_path(#user), :html => {:method => :post} do |f| %>
<%= f.label :name %>
<%= f.text_field :name, class: "form-control", placeholder: 'Name' %>
<%= f.label :email%>
<%= f.text_field :email, class: "form-control", placeholder: "Email" %>
<%= f.label :address%>
<%= f.text_field :address, class: "form-control", placeholder: "Address" %>
<%= f.label :phone %>
<%= f.text_field :phone, class: "form-control", placeholder: "Phone Number" %>
<%= f.submit "Apply"%>
<%end%>
</div>
</div>
</div>
</div>
</div>
Also, when the submit button fails, nothing happens. No error messages, no console errors. Nothing.
Take method out from the html hash?
form_for #user, url: users_path(#user), :method => :post do |f|
Try this:
<%= form_for #user, :url => {:controller => "your-controller-name", :action => "your-action-name"} do |f| %>
Can you try this one:
<%= form_for #user, url: {action: "create"} do |f| %>
...
...
<%= f.submit "Apply" %>
<% end %>
But I strongly suggest you to use simple_form gem.

Resources