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"
Related
I have a Rails 3 application with a view that is irritating me. The view is essentially four tabs. Each tab shows a filtered list of records like this:
Tab One
<% #faults.each do |fault| %>
<%= fault.name %>
<% end %>
Tab Two
<% #faults_pending.each do |fault| %>
<%= fault.name %>
<% end %>
Tab Three
<% #faults_closed.each do |fault| %>
<%= fault.name %>
<% end %>
Obviously there is a lot more code that I have missed out. To tidy the view up significantly I considered making a partial for each tab, however, each of those partials would be identical except for the <% #faults.each do |fault| %> line.
Is there a way of having one partial and somehow when rendering the partial set the scope (or method, I can never remember what it's called).
I think you're looking for render's :locals option.
Tab one
<%= render :partial => "faults", :locals => { :faults => #faults } %>
Tab two
<%= render :partial => "faults", :locals => { :faults => #faults_pending } %>
Tab three
<%= render :partial => "faults", :locals => { :faults => #faults_closed } %>
Partial
<% faults.each do |fault| %>
<%= fault.name %>
<% end %>
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.
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 .
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?
I have a REST resource called Greetings.
Here is my routes file:
resources :greetings
I have a form to create or update a greeting as follows:
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form') do %>
<%= text_area :greeting, :content, :rows => 3, :placeholder => "Type your message..." %>
<% end %>
note: I am using form_tag because this form also collects user data.
This is supposed to create a hidden field with method=> put but its not so it can't find the route.
Any ideas how I can get this to submit to the update action?
Just write
<%= form_tag(#greeting, :remote => true, :id => 'greeting_form', :method => :put) do %>
and everything should be working.
You can use form_for tag and still collect user data like this:
<%= form_for #greeting, :validate => true do |f| %>
<%= f.text_area :content, :rows => 3, :placeholder => "Type your message..." %>
<%= f.fields_for #user do |u| %>
<%= u.text_field :name %>
<% end %>
<% end