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
Related
I have a parent (playlist) -> child (tracks) structure in my rails app.
What I am doing in the form is showing attributes from the parent and then I have a table for the child attributes.
<%= form_for(#playlist) do |f| %>
<% if #playlist.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#playlist.errors.count, "error") %> prohibited this playlist from being saved:</h2>
<ul>
<% #playlist.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label "Playlist Name: " %>
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label " Image: " %>
<%= f.file_field :photo %>
</div>
<br>
<div class="field">
<h5> description: </h5>
<br>
<%= f.text_area :description, :size => "80x3"%>
</div>
<br>
<div>
<h5> Add some music: </h5>
</div>
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
<% if !#playlist.tracks.blank? %>
<table id="tracks" class="display">
<thead>
<tr>
<th>Delete</th>
<th>Track</th>
<th>Album</th>
<th>Artist</th>
<th>Label</th>
</tr>
</thead>
<tbody>
<%= f.fields_for :tracks do |ff| %>
<%= render "track_fields", :f => ff %>
<% end %>
</tbody>
</table>
<% end %>
<div class="actions">
<%= f.submit "Save" %>
</div>
<% end %>
I am using paperclip to upload the files. I am uploading images on playlist model and mp3s on the tracks model.
The code in question is the following:
<div class="field">
<%= f.fields_for :tracks, Track.new do |ff| %>
<%= ff.file_field :audio, :multiple => true %>
<% end %>
</div>
I feel this is a hack. Why do I have to add a new track for me to show the file_field (choose file button) once? Towards the bottom of the form, you will notice that I have to call
<%= f.fields_for :tracks do |ff| %>
again because I want to iterate through all the tracks for the playlist and show them in a table.
Not sure if this question makes sense, but how do I show the file_field once (I feel Track.new is a hack and I think its messing up in saving the model)?
EDIT:
here is a screenshot of what happens if I don't have Track.new:
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 %>
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
Am trying to hide this publish 'tick-box' from non-admin users. I used the CanCan plug-in and set up the correct permissions but am struggling with the code syntax. I have used <%= if can? :publish, article %> in the views/articles/_form.html.erb partial but it doesn't work?
<div class="field">
<%= f.label :tag_names, "Tags" %> <br />
<%= f.text_field :tag_names %>
</div>
<div class="field">
<%= check_box("article", "published" ) %>
**<%= if can? :publish, #article %>**
<%= "Publish article" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
You should be using <%, not <%=. Also, the if statement is in the wrong place, and there's no closing end statement. Here's correct code:
<div class="field">
<%= f.label :tag_names, "Tags" %> <br />
<%= f.text_field :tag_names %>
</div>
<% if can? :publish, #article %>
<div class="field">
<%= check_box("article", "published" ) %>
<%= "Publish article" %>
</div>
<% end %>
<div class="actions">
<%= f.submit %>
</div>
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.