Nested form with polymorphic association not displaying fields - ruby-on-rails-3

I have a model with a has_one and polymorphic association like this:
class Disc < ActiveRecord::Base
has_one :item, :as => :article, :dependent => :destroy
accepts_nested_attributes_for :item
end
class Item < ActiveRecord::Base
belongs_to :article, :polymorphic => true
end
I am trying to have a nested form, but only the fields for disc are being displayed and not the ones for item. This is my form:
<%= form_for(#disc) do |d| %>
<% d.fields_for :item do |f| %>
<div class="field">
<%= f.label :title %><br />
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :stock %><br />
<%= f.number_field :stock, :min => 0 %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<% end %>
DISC:
<div class="field">
<%= d.label :num_discs %><br />
<%= d.number_field :num_discs %>
</div>
<div class="field">
<%= d.label :audio %><br />
<%= d.text_field :audio %>
</div>
<div class="field">
<%= d.label :subtitles %><br />
<%= d.text_field :subtitles %>
</div>
<div class="actions">
<%= d.submit %>
</div>
<% end %>
No errors are shown in the console.

Try adding an = before the first <% on the line <% d.fields_for :item do |f| %>
. I haven't used erb in awhile though so this might not be the issue

Related

Data does not appear in table

I have a simple form that allows you to select users and select a hospital. The form successfully submits. However when I view the index.html.erb, it looks like the following: [imgur][1]. Any idea why the "Full name" and "Hospital" is not appearing?
<div class="field">
<%= f.label :booking_reference %>
<br/>
<%= f.text_field :booking_reference %>
</div>
<div class="field">
<%= fields_for :User do |user| %>
<%= user.collection_select :user_id, User.all, :id, :full_name %>
<% end %>
</div>
<div class="field">
<%= fields_for :hospital do |hosp| %>
<%= hosp.collection_select :hospital_id, Hospital.all, :id, :name %>
<% end %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Wouldn't that be like
<div class="field">
<%= f.collection_select :hospital_id, Hospital.all, :id, :name %>
</div>
and
<div class="field">
<%= f.collection_select :user_id, User.all, :id, :full_name %>
</div>
?
Otherwise you break the form helper.
In doubt, check the api here: collection_select

json format in ruby on rails?

How to send data in database and show in needed part by using json format in ruby on rails3?
my form is like this
<%= 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 |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 :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :login %><br />
<%= f.text_field :login %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
In the controller:
def show
#user = User.find(params[:user_id]
respond_to do |format|
format.json { :render => #user }
end
end
You can customise the JSON output by overriding the as_json in the User model:
def as_json(options=false)
self.include_root_in_json = false
options = (options || {}).reverse_merge(
:except => [:updated_at, :created_at, :id],
:include => :some_association
)
super(options)
end

Rails 3 Nested Attributes

I've had this issue for a few days and looked over every tutorial and stack overflow. I cannot seem to resolve. I can't seem to use nested attributes that work. I get an error with this form view.
**Form View**
<%= form_for #trip, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :start, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :start, :class => 'text_field' %>
</div>
<div class="control-group">
<%= f.label :end, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :end, :class => 'text_field' %>
</div>
<div class="control-group">
<% f.fields_for :driver do |d| -%>
<%= d.label :driver, :class => 'control-label' %>
<div class="controls">
<%= d.text_field :driver, :class => 'text_field' %>
</div>
Model:
class Trip < ActiveRecord::Base
attr_accessible :end, :start
belongs_to :driver
belongs_to :customer
accepts_nested_attributes_for :driver
end
class Driver < ActiveRecord::Base
attr_accessible :name
has_many :trips
end
As you said you need closing end tag.
If you have the "Can't mass-assing procteted attributes: drivers" error you must declare "attr_accessible" on :drivers_attributes at the trip model.

Rails 3.0 : form_tag How to convert this form_for to form_tag.

This is the login page for my app but the role selected for a user is not being reflected in the database so i want to try changing form_for into form_tag to see if the data gets submitted.
<%= 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
<%= form_tag users_path, methods=> post:do %>
<% 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 |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= label_tag 'email' %><br />
<%= text_field_tag :email, params[:email], :placeholder => "Email" %>
</div>
<div class="field">
<%= label_tag 'username' %><br />
<%= text_field_tag :username, params[:username], :placeholder => "Username" %>
</div>
<% if #current_method == "new" %>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag 'password' %>
</div>
<div class="field">
<%= label_tag :password_confirmation %><br />
<%= f.password_field_tag 'password_confirmation' %>
</div>
<% end %>
<% for role in Role.find(:all) %>
<div>
<%= check_box_tag "user[role_ids][]", role.id, #user.roles.include?(role) %>
<%= role.name %>
</div>
<% end %>
<div class="actions">
<%= submit_tag 'submit' %>
</div>
<% end %>

Rails 3 dropbox help

i did a scaffold to make my DB and menus for group i need a drop down box instead of a text field
here is the code i have atm
<%= form_for(#ad) do |f| %>
<% if #ad.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#ad.errors.count, "error") %> prohibited this ad from being saved:</h2>
<ul>
<% #ad.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :ad_name %><br />
<%= f.text_field :ad_name %>
</div>
<div class="field">
<%= f.label :group %><br />
<%= f.text_field :group %>
</div>
<div class="field">
<%= f.label :description %><br />
<%= f.text_area :description %>
</div>
<div class="field">
<%= f.label :credits %><br />
<%= f.text_field :credits %>
</div>
<div class="field">
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
if you can let me know how to change group to a pull down with selections it would be helpful.
Have you tried changing f.text_field to f.select?
You need something like
<div class="field">
<%= f.label :group %><br />
<%= f.select :group, Group.all.collect{|g| [g.id, g.name]} %>
</div>
That is to say, pass as second argument the list [id, label] of the field you want to generate.