Rails 3 dropbox help - ruby-on-rails-3

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.

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

Paperclip error rails 3

So I have been trying to follow some paperclip tutorials and adjusting them to rails 3. I follow the steps and got an error once I started to add the code needed for the _form and show.htm.erb files. This is the error I get.
Error Message:
ActionView::Template::Error (undefined method `label' for nil:NilClass):
11: </div>
12: <% end %>
13: <div class="field">
14: <%= form.label :photo, "Photo" %>
15: <%= form.file_field :photo %>
16: </div>
17: <div class="field">
Form:
<%= form_for #user, :html => { :multipart => true } do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
<div>
<% end %>
<div class="field">
<%= form.label :photo, "Photo" %>
<%= form.file_field :photo %>
</div>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.text_field :password %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>`
form.label doesn't make sense, since you're calling your form variable f inside your block, with the line form_for ... do |f|.
You need to use f.label etc.

Hide 'Publish' Button From non Admins?

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>