Undefined Local Variable or Method `f' In a view partial - ruby-on-rails

This seems like such a stupid, easy fix (it probably is) but I've been searching SO and other areas on the web with no luck. I'm getting an undefined method local variable error 'f' within my partial used in my view. I'm assuming that the "do block" is ending somehow prior to reaching the partial but I'm not 100% certain.
Partial
<% if user_admin_or_premium? %>
<div class="form-group">
<%= f.label :private, class: 'checkbox' do %>
<%= f.check_box :private, :true %> Private Wiki?
<% end %>
</div>
<% end %>
View
<div class="col-md-8">
<%= form_for #wiki do |f| %>
<div class="form-group">
<%= f.label :title %>
<%= f.text_field :title, class: 'form-control', placeholder: "Enter Wiki Title" %>
</div>
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, rows: 10, class: 'form-control', placeholder: "Enter Wiki Body" %>
</div>
<%= render partial: "wikis/form", f: f %>
<div class="form-group">
<%= f.submit "Save", class: 'btn btn-success' %>
</div>
<% end %>
</div>
Full Error
NameError in Wikis#new
undefined local variable or method `f'
<% if user_admin_or_premium? %>
<div class="form-group">
<%= f.label :private, class: 'checkbox' do %>
<%= f.check_box :private, :true %> Private Wiki?
<% end %>
</div>

Omit the partial: key from your render function:
<%= render "wikis/form", f: f %>
Easy fix, but certainly not obvious, I've been stumped by the same issue before. The above statement is equivalent to the following:
<%= render partial: "wikis/form", locals: { f: f } %>

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: split a nested form into partials

I have a nested form which works perfectly fine, however, I'm trying to figure out how to split that nested form into a partial.
original form:
<%= form_for(#user) do |f| %>
<%= f.fields_for :achievements, Achievement.new do |ff| %>
<div class="field">
<%= ff.label :certification_name, 'Cert Name' %>
<%= ff.text_field :certification_name %>
</div>
<% end %><!-- fields_for -->
<%= f.submit 'Save', id: "submit-achievement", class: 'btn btn-primary' %>
Here's what I'm trying to do....
<%= form_for(#user) do |f| %>
<%= f.fields_for :achievements, Achievement.new do |ff| %>
<%= render partial: 'achievements/new_certification' %>
<% end %><!-- fields_for -->
<%= f.submit 'Save', id: "submit-achievement", class: 'btn btn-primary' %>
here's the partial.
<div class="field">
<%= ff.label :certification_name, 'Cert Name' %>
<%= ff.text_field :certification_name %>
</div>
the problem is that it doesn't know what to do with the 'ff' variable.
ActionView::Template::Error (undefined local variable or method `ff' for #<#<Class:0x007fdccd5498c0>:0x007fdcc04894d8>):
You can achieve this by passing a local variable to your partial like so:
<%= render partial: 'achievements/new_certification', locals: {ff: ff} %> # not a fan of the naming
Then in achievements/_new_certification.html.erb
<div class="field">
<%= ff.label :certification_name, 'Cert Name' %>
<%= ff.text_field :certification_name %>
</div>
Documentation on partials

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.

If statement preventing submit button to work

My submit button won't work because of the first if statement. If I remove the block, the button works. The statement seems to break the view. Why?
<% if params[:action] == "edit" %>
<div class="field">
<%= form_tag :action => "edit" do %>
<%= select_tag :vehicle_id, options_from_collection_for_select(#vehicles, :id, :model, params[:id].to_i), :onchange => "this.form.submit()" %>
<% end -%>
<%= link_to 'Nouvelle voiture', new_vehicle_path %>
</div>
<% end -%>
<div class="field">
<%= f.label "Modèle" %>
<%= f.text_field :model, required: true %>
</div>
<div class="field">
<%= f.label "Immatriculation" %>
<%= f.text_field :license_plate, required: true %>
</div>
<div class="field">
<%= f.label "Complément" %>
<%= f.text_field :complement, required: true%>
</div>
<div class="field">
<%= f.label "Puissance CV" %>
<%= f.number_field :horse_power, required: true %>
</div>
<div class="field">
<%= f.label "Indemnité KM" %>
<%= f.number_field :km_compensation, required: true%>
</div>
<% if params[:action] == "edit" %>
<%= link_to 'Détruire', #vehicle, method: :delete, data: { confirm: 'Êtes-vous sûr ? Les trajets associés seront aussi détruits.' } %>
<h1>Trajets</h1>
<div>
<span>Clients</span>
<span>Kms aller/retour</span>
</div>
<%= f.fields_for :trip_distances do |builder| %>
<div class="field">
<%= builder.text_field :id_contract %>
<%= builder.number_field :length %>
</div>
<% end -%>
<% end -%>
<div class="actions">
<%= f.submit 'Sauvegarder' %>
</div>
When you put if statement you don't have form in which your submit button resides. Without if statement your form is in the view, and you can submit to it.
Found the solution. I didn't need the form_tag:
<%= form_tag :action => "edit" do %>

Resources