partials inside a view undefined method errors - ruby-on-rails

I am adding partials to a show view, but I am getting errors of undefined methods that are inside the partials that are being called. I use the partials in other places and it works fine. I think I need to define a local variable, but I am not sure which one
user/show.html.erb
<h2>Copy To Approve</h2>
<div id="basic_details" class="idea-show-columns">
<%= render :partial => "/ideas/idea_basic_show" %>
<%= render :partial => "/ideas/comments" %>
<%= render :partial => "/ideas/mockups"%>
</div>
<div id="copy_details" class="idea-show-columns">
<%= render :partial => "/ideas/copy_show" %>
</div>
partial: ideas/idea_basic_show.html.erb
<fieldset data-model="idea-basic" class="idea-edit">
<h2>Basic Idea Specs</h2>
<div data-attribute="product_sku" class="edit-field">
<%= label :sku, "Product SKU" %>
<%= f.text_field :sku %>
</div>
<div data-attribute="working_name" class="edit-field">
<%= label :working_name, "Working Name" %>
<%= f.text_field :working_name %>
</div>
<div data-attribute="priority" class="edit-field">
<%= f.label :priority, 'Priority Level' %>
<%= f.select :priority, Idea::PRIORITIES.collect{ |level, label| [label, level]} %>
</div>
<% if current_user.has_overlord_access? %>
<div data-attribute="overlord_id" class="edit-field">
<%= f.label :overlord_id, 'Sign Off' %>
<%= f.select :overlord_id, User.overlords.collect{|o| [o.full_name, o.id]},
:include_blank => true %>
</div>
<% end %>
<br data-clear="all" />
<div data-attribute="working_description" class="edit-field">
<%= label :working_description, "Working Description" %>
<%= f.text_area :working_description %>
</div>
</fieldset>
the errors I am getting in this partial specifically is :
undefined method `sku' for nil:NilClass
Like I said I think I need to define a local in my partial I just don't know the syntax

The locals syntax like this
<%= render :partial => "/ideas/copy_show", :locals => {:f => f} %>
now use f in your partial page

Related

Undefined Local Variable or Method `f' In a view partial

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 } %>

Prepopulate complex form values in rails edit view

I have an assessment form that renders a list of questions, and accepts answers for each question. The form data is submitted manually via ajax.
I'm having an issue when trying to pre-populate the data in my edit view. Here's how my views look:
edit.html.erb:
<div class="container">
<%= render 'assessment_form', :locals=> {:assessment => #assessment} %>
</div>
_assessment_form.html.erb:
<%= form_for #assessment do |f| %>
<%= f.submit 'Save', :class=> "btn btn-primary", :style=>"width: 100%;" %>
<div class="col-sm-2 col-sm-offset-10">
<h4>Show on Followup?</h4>
</div>
<% #assessment.questions.where(category:"S").each do |question| %>
<%= render 'question_fields', :question=> question, :f=> f %>
<% end %>
<%= f.hidden_field :patient_id, :value=> #assessment.patient.id %>
<%= f.hidden_field :template_id, :value=> #assessment.template_id %>
<% end %>
_question_fields.html.erb:
<p><%= question.content %></p>
<%= f.fields_for :answer do |builder| %>
<div class="row question">
<div class="col-sm-2 pull-right toggle">
<%= builder.check_box :tracking, {:checked=> false, :class=>"trackable", :data => {:'on-text' => "Yes", :'off-text' => "No", :'on-color'=> "success", :question => question.id}}, 0 , 1 %>
</div>
<div class="col-sm-10">
<%= builder.text_area :content, :class=>"form-control question-field", :data => {:question => question.id} %>
</div>
</div>
<% end %>
But none of the answers content are rendering in the view, the form fields are blank.
Here's an example call: Assessment.last.answers.first.content will give me the string "test". That should be showing up in my text_area box but it's not.
Just do, the instance variable #assessment is passed to the partial.
<%= render 'assessment_form' %>

Select tag rails

I have a view where I need to show a list of pages (I have a model called Page) and then i need the id of the selected page
I have this code in the view
مهمة جديدة
: <%= form_for :task, :url => {:action=>"create", :controller=>"tasks"}, :html => {:class :
=> "nifty_form"} do |f| %>
<% if #task.errors.any? %>
<div id="error_explanation">
<h2>: عذرا لم نستطع استكمال طلبك</h2>
<ul>
<% #task.errors.full_messages.each do |msg| %>
<li style="color:red;"><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "اسم المهمة" %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label "وصف المهمة" %><br />
<%= f.text_area :description %>
</div>
<div class="actions">
<button><%= link_to 'العودة الى قائمة المهام', project_tasks_path %></button>
<%= f.submit "مهمة جديدة"%>
</div>
<%= select_tag(:page, options_for_select(['a',1])) %>
<% end %>
#pageslist is a 2d array of the form [['pagename', 1], ..]
The question is how can i access the page_id of the selected page in the controller ?
I tried params[:p_id] but it is not working any help please?
This is what I have in the controller:
#task = Project.find(params[:project_id]).tasks.new(params[:task])
#task.update_attributes({:page_id => params[:p_id]})
Assuming that #pagelist is in a format as you described, then in your view:
<%= select_tag(:page, options_for_select( #pagelist )) %>
You had missed it in your example, and provided only 1d array for options_for_select.
With such prepared view your create action will receive page id in:
params[:page]
it's because you had passed :page as a first argument for select_tag.

Rails3 form partial reuse

The following is the Rails std form that works.
<div id="content-form">
<%= form_for #passion, url: passion_path do |f| %>
<%#= render :partial => "form", :f => f %>
<div class="alternate">
<%= f.label "Status" %>
<%= f.text_field :status %>
</div>
<% end %>
</div>
Now I extract the partial to _form.html.erb with the following:
<div class="alternate">
<%= f.label "Status" %>
<%= f.text_field :status %>
</div>
And update the form as:
<div id="content-form">
<%= form_for #passion, url: passion_path do |f| %>
<%= render :partial => "form", :f => f %>
<% end %>
</div>
But now this throws an error complaining about the f variable.
undefined local variable or method `f' for #<#<Class:0x0000012d51d3e8>:0x0000012d796ef8>
Extracted source (around line #2):
1: <div class="alternate">
2: <%= f.label "Status" %>
3: <%= f.text_field :status %>
4: </div>
Why is this? Its pretty Rails basic.
One thing to consider that the resource #passion is singular. i.e. resource :passion (user has_one passion)
Due to that, I've to use explicit url: passion_path in the form_for ....
Btw, using Rails 3.2.9
try this
<div id="content-form">
<%= form_for #passion, url: passion_path do |f| %>
<%= render :partial => "form", :locals=>{:f => f } %>
<% end %>
</div>
You are using the :partial option, which means you have to pass local variables in with :locals:
<%= render :partial => "form", :locals => { :f => f } %>
The less verbose option is:
<%= render "form", :f => f %>

Hartl's Railtutorial: Converting error-msg partial to all object has undefined method 'f' error

Following Hartl's railstutorial and I changed my error-messages partial to work with other objects http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec:edit_form.
After changing all my #user to object and passing :object => f.object when I render the partial, I get this error when I try to load up my sign up page:
NameError in Users#new: undefined local variable or method 'f'
and it points to this line
<%= render 'shared/error_messages', :object => f.object %>
Any advice?
new.html.erb
<div id="mainlogo"><%= image_tag("sample_logo.png", :alt => "Sample Title",
:class =>"round")%></div>
<div id="errors_new">
<%= render 'shared/error_messages', :object => f.object %>
</div>
<div id="signup_spacer"></div>
<div id="signupcontainer_new" class="round">
<h1>Sign Up</h1>
<p>It's free</p>
<div id="signupform_new">
<%= render :partial => 'users/usersignup' %>
</div>
</div>
_error_messages.html.erb
<% if object.errors.any? %>
<div id="error_explanation" class="round">
<% object.errors.full_messages.each do |msg| %>
<h2><%= msg %></h2>
<% end %>
</div>
<% end %>
_usersignup.html.erb
<%= form_for(#user) do |f| %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :name %>
</div>
...
<% end %>
f.object is only valid inside the block that defines f, which is now in the form partial.
The line needs to be within the form_for block in the form partial as in the tutorial.
The tutorial also creates a partial for just the fields, where that line can go.

Resources