rails 3 form_for undefined variable error - ruby-on-rails-3

I got a undefined variable or method error when I try to render a partial view which is a form. The code like following:
in a partial view file, show.html.erb:
<ul class="users">
<% #users.each do |user| %>
<%= render 'pending' %>
<% end %>
</ul>
in _pending.html.erb:
<%= form_for current_user.friendships.build(:friendb_id => user.id) do |f| %>
<div><%= f.hidden_field :friendb_id %></div>
<div class="actions"><%= f.submit "Confirm" %></div>
<% end %>
The rspec error is:
undefined local variable or method `user'
I tried:
<%= render :partial => 'pending', :locals => {:user => user} %>
but it still doesn't work
Anyone knows why the form cannot find user variable?

Related

POST/PUT not working with partial in Rails form

I am using a single form for new and edit actions in my controller.
edit.html.erb
<%= form_for #user, :url => edit_users_path(#user) do |f| %>
<%= render :partial => 'form', :object => f %>
<%= f.submit "Update" %>
<% end %>
new.html.erb
<%= form_for #user, :url => users_path do |f| %>
<%= render :partial => 'form', :object => f %>
<%= f.submit "Submit" %>
<% end %>
_form.html.erb
<%= form.text_field :firstname %>
<%= form.text_field :lastname %>
<%= form.email_field :email %>
For boths the actions, submit doesn't work. The form is rendered fine. I added data from rails console to check for edit. It doesn't work either.
If I create 2 separate form for each, new & edit, it works fine. So i assume my controller code is fine.
Thanks
Render the partial with:
<%= render :partial => 'form', :form => f %>
instead. If it doesn't work, check the input names in the generated HTML.
I see in your edit and new files are using an |f| variable for the form builder but in the form partial you're using form as the variable. Try changing the fields in your partial to "f.text_field"

Creating nested form in Rails 3.1

I am trying to render a partial which I have set up as the following. I have am also trying to create a nested form whereby I have included accepts_nested_attributes_for :user in my hospital_bookings model. I seem to be getting the following error:
NameError in Rota_days#index
Showing
C:/Users/home/Desktop/Portal/app/views/rota_days/index.html.erb
where line #31 raised:
undefined local variable or method `hospital_booking' for
which is pointing to the following line <%= render :partial => "booking_dialog", :locals => { :booking => hospital_booking.new } %> of my index.html.erb as shown below. I thought it was something to do with my pluralization. By changing hospital_bookings.new to hospital_booking.new but this did not work
_booking_dialog.html.erb
<%= form_for booking do |f| %>
<%= f.fields_for :user do |f| %>
<br/>
<%= f.label :name %>
<br/>
<%= f.text_field :name %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
hospital_booking.new is nonsensical: you have no local variable named hospital_booking. If you want a new instance of the HospitalBooking model, then you want HospitalBooking.new.
So:
<%= render :partial => "booking_dialog", :locals => { :booking => HospitalBooking.new } %>
Update (from the comments)
In the booking_dialog form partial, you need to put the name attribute on the associated user record inside a fields_for block, to distinguish it from the fields for the parent (booking):
<%= form_for booking do |f| %>
<%= fields_for :user do |user_fields| %>
<%= user_fields.label :name %>
<%= user_fields.text_field :name %>
<% end %>
<%= f.hidden_field :hospital_id %>
<%= f.hidden_field :id unless booking.new_record? %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
p.s. it seems very strange that you have a hidden field for the :id in here. You shouldn't need that.

rails form partial for new and edit on same page

I am running into issues trying to separate a form from a view into a partial. I want to use the same form for the new and edit views. These are both on the same page. The new model form is at the top of the page and uses a variable that I set in the controller.
<%= form_for #new_hire do |f| %>
<%= render :partial => 'new_hire_requests/form', :locals => {:f => f} %>
<% end %>
I then have a partial for the pending approvals that gets rendered by another partial
<%= render :partial => 'pending_approval', :collection => #pending_approval %>
And inside the pending approval partial I have this
<%= form_for pending_approval do |f| %>
<%= render :partial => 'new_hire_requests/form', :locals => {:f => f} %>
<% end %>
This is throwing an error
undefined method `new_hire_request_path' for #<#<Class:0x0000010488ac98>:0x0000010223ffc0>
Is there a way to re use the form code for both a new and edit form on the same page?
Controller Logic
#new_hire = NewHireRequest.new
#new_hire_requests = current_user.new_hire_requests
#pending_approval = #new_hire_requests.select{|p| p.status == 'pending_hr_approval' || p.status == 'pending_exec_approval'}
Partial code
<%= render 'shared/error_messages', object: f.object %>
<fieldset class="first">
<%= f.label :first_name, "First Name" %>
<%= f.text_field :first_name %>
</fieldset>
<fieldset>
<%= f.label :last_name, "Last Name" %>
<%= f.text_field :last_name %>
</fieldset>
<%= f.submit "Submit for Approval <i class='icon-share-alt icon-white'></i>",
class: "button_green" %>
add resources new_hire_requests in the routes and get done with it .

Ruby on Rails Tutorial chapter 9

I am confused by the addition of [:session] to params. It also seems to break my website. Can someone please explain what this does for me?
class SessionsController < ApplicationController
.
.
.
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
.
.
.
end
Error message:
1) Authentication signin with invalid information
Failure/Error: before { click_button "Sign in" }
NoMethodError:
undefined method []' for nil:NilClass
# ./app/controllers/sessions_controller.rb:7:increate'
# (eval):2:in click_button'
# ./spec/requests/authentication_pages_spec.rb:18:inblock (4 levels) in '
EDIT 8/2
I believe the problem is related to a switch from form_for to form_tag. I lost the reference to sessions in the switch because I could not figure out how to properly include it. If anyone has any advice on this issue it would be most appreciated. I am wondering if there is a practical reason for wanting it to be params[:session][:email] instead or is it just for organization?
new.html.erb
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_tag sessions_path do %>
<%= label_tag :email %>
<%= text_field_tag :email %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
Try removing the [:session] brackets, that worked for me
It brakes your code 'cos the params[:session] is nil I think and you trying to get [:email] from nil, what ofcourse should cause the exception. There should be some code in tutorial that defines params[:session] hash. Try to look better.
To make your code stable you have to be sure that params[:session] is always defined or try to use ternar function params[:session] ? params[:session][:email] : ''
You can replace this in your view :
<%= form_for :session, :url => sessions_path do %>
<%= label_tag :email %>
<%= text_field_tag :email %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
form_tag generate just an HTML form tag and form_for is used to describe something. All the inputs for a field of a form create with form_for will have a name like this : user_session[email]. So, when you submit the form, in your controller, you will have this : params[:user_session][:email].

error partial for multiple models Rails 3

I have a partial that I use to show errors from an object onto a form.
<% if object.errors.any? %>
<div id="error_explanation">
<h2>Oops, looks like <%= pluralize(object.errors.count, "error") %>
occured:</h2>
<br />
<ul>
<% object.errors.each do |key, msg| %>
<li><%=key%><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
It works great for 1 model.
However I can't figure out how to make it work for a form that has two models.
Any ideas? I don't want to use the plugin I would like to have more control.
Just output the partial for each model in the form view with 2 models and pass actual model instances into this partial as local variable:
<%= render :partial => 'name_of_partial_to_show_model_errors', :locals => {:object => #model1} %>
<%= render :partial => 'name_of_partial_to_show_model_errors', :locals => {:object => #model2} %>