undefined local variable or method `f' in Rails and Paperclip - ruby-on-rails

I'm trying to create a new map, and it's not working out so far:
undefined local variable or method `f' for #<#<Class:0x007ff46c0b12c0>:0x007ff46d777ba8>
I'm using Paperclip. The new page form:
<%= form_for #map, :url => maps_path, :html => { :multipart => true } do |form| %>
<% if #map.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#map.errors.count, "error") %> prohibited this map from being saved:</h2>
<ul>
<% #map.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :carname %><br />
<%= f.text_field :carname %>
</div>
<%= form.file_field :map %>
<div class="field">
<%= f.label :criticalcomponentlocations %><br />
<%= f.text_area :criticalcomponentlocations %>
</div>
<div class="field">
<%= f.label :warnings %><br />
<%= f.text_area :warnings %>
</div>
<div class="field">
<%= f.label :additionalinfo %><br />
<%= f.text_area :additionalinfo %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>

Since you pass form variable to the block passed to form_for method, you should substitute f with form.

Related

RoR scaffold error, 'undefined method to_datetime for 0:Fixnum'

I'm new on RoR and then to scaffold a table with datetime type, the following error appear when I enter to generated 'New' page form:
undefined method `to_datetime' for 0:Fixnum
<%= form_for(#alumno) do |f| %>
<% if #alumno.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#alumno.errors.count, "error") %> prohibited this alumno from being saved:</h2>
<ul>
<% #alumno.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :nombres %><br>
<%= f.text_field :nombres %>
</div>
<div class="field">
<%= f.label :apellido_paterno %><br>
<%= f.text_field :apellido_paterno %>
</div>
<div class="field">
<%= f.label :apellido_materno %><br>
<%= f.text_field :apellido_materno %>
</div>
<div class="field">
<%= f.label :dni %><br>
<%= f.text_field :dni %>
</div>
<div class="field">
<%= f.label :usuario %><br>
<%= f.text_field :usuario %>
</div>
<div class="field">
<%= f.label :usuario_personal %><br>
<%= f.text_field :usuario_personal %>
</div>
<div class="field">
<%= f.label :pass %><br>
<%= f.text_area :pass %>
</div>
<div class="field">
<%= f.label :fecha_registro %><br>
<%= f.datetime_select :fecha_registro %> /*error happens here*/
</div>
<div class="field">
<%= f.label :fecha_modificacion %><br>
<%= f.datetime_select :fecha_modificacion %>
</div>
<div class="actions">
<%= f.submit %>
</div>
I found similar questions but don't know exactly how repair the issue.
RoR version 4.0.0
ruby version 2.3.1p112
The code you posted doesn't show where the error is occurring, but the bottom line is that you're trying to call to_datetime on an integer value. to_datetime, however, is a member of String, not integer. Whatever you're trying to convert into a DateTime object must be a String.
If you post the code where the error actually occurs, I can give you more specific direction, but what I've posted so far should be enough to solve the problem on your own.

Rails Display Form Error Below Each Field

I have the following form
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<%= #user.errors.messages[:name] %>
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br>
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :phone %><br>
<%= f.text_field :phone %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
I want to display form error for each field below it instead of everything on the top. Went through rails guide but was not able to figure how to do it.
You can write a helper to return error message for any field of any object
In /app/helpers/application_helper.rb:
def show_errors(object, field_name)
if object.errors.any?
if !object.errors.messages[field_name].blank?
object.errors.messages[field_name].join(", ")
end
end
end
In view file:
<div class="field">
<%= f.label :name %><br>
<%= f.text_field :name %>
<p class='error'><%= show_errors(#user, :name) %></p>
</div>

Rails: How to make a form post to another controller action

I know you are usually supposed to use the linking between new/create and edit/update in rails, but I have a case where I need something else. Is there anyway I can achieve this same connection?
I have a form for a model and I want it to post the data (similar to how a new view, post to the create action).
Here is my form
<div id="new-job">
<%= form_for(#job) do |f| %>
<% if #job.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#job.errors.count, "error") %> prohibited this job from being saved:</h2>
<ul>
<% #job.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :location %><br />
<%= f.text_field :location %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<%= f.submit "create job", id: "submit-job", class: "button small radius" %>
<%= link_to "go back", jobs_path, class: "button small radius secondary" %>
<div id="new-job-errors"> </div>
</div>
<% end %>
</div>
Use the :url option.
= form_for #job, :url => company_path, :html => { :method => :post/:put }

Update record error when combined with delete

I have a problem at the moment with my _form.html.erb for editing a game. My code for this class is as followed:
<%= form_for #game, :html => { :multipart => true } do |f| %>
<% if #game.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#game.errors.count, "error") %> prohibited this game from being saved:</h2>
<ul>
<% #game.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<div id="p1"><%= f.label :game_name %> </div>
<%= f.text_field :game_name %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :genre %> </div>
<%= f.text_field :genre %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :console %> </div>
<%= f.select :console, Game::CONSOLE_OPTIONS, :prompt => 'Select' %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :condition %> </div>
<%= f.text_field :condition %>
</div>
<br />
<div class="field">
<div id="p1"><%= f.label :description %> </div>
<%= f.text_area :description, :rows => 6 %>
</div>
<%= f.file_field :photo %>
<br />
<div class="actions">
<%= f.submit %>
<%= button_to 'Destroy', root_url, :confirm => 'Are You Sure?', :method => :delete %>
</div>
<% end %>
The problem I have is as followed. If I click the delete button it will delete the record no problem, but if I click the update button it removes the record also. If I take out the delete command then it updates fine. How can I get both to display together whilst doing their individual duties?
You need extract your destroy button from your form_for of update.

Nested form using paperclip

I have a model called posts, and it has many attachments.
The attachments model is using paperclip.
I made a standalone model for creating attachments which works just fine, this is the view as instructed here (https://github.com/thoughtbot/paperclip):
<% form_for :attachment, #attachment, :url => #attachment, :html => { :multipart => true } do |form| %>
<%= form.file_field :pclip %>
<%= form.submit %>
<% end %>
The nested form in posts looks like this:
<% #attachment = #posts.attachments.build %>
<%= form_for(#posts) do |f| %>
<% if #posts.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#posts.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% #posts.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.fields_for :attachments, #attachment, :url => #attachment, :html => { :multipart => true } do |at_form| %>
<%= at_form.file_field :pclip %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
An attachment record is created, but its empty. The file is not uploaded. The post meanwhile, is successfully created...
Any ideas?
You are missing the :multipart option in your form definition:
<% #attachment = #post.attachments.build %>
<%= form_for #post, :html => { :multipart => true } 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.fields_for :attachments, #attachment do |at_form| %>
<%= at_form.file_field :pclip %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Also, your #posts variable should really be #post (single ActiveRecord instance as opposed to an array of ActiveRecord instances).

Resources