I am new to Rails so this is probably an easy question but can't find anything that seems to explain it well.
How do I style forms in rails to. When I try to use the default styling for input (for example) it styles my text field and create button the same. Is there a built in convention or do I have to add some sort of helper. If it is a helper can you you walk me through that as I have not done it before.
Here is my form:
<% form_for (Post.new) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :BattleCry %>
<br/><i>Between 25 and 300 words</i><br/>
<%= f.text_area :body %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %><hr/>
The most appropriate way would likely be just giving them different classes and styling them via usual css methods.
Eg.
<%= text_field( :title, :class => "something" ) %>
I think this is more of a CSS question. You seem to be on the right track ruby-on-rails-wise. Both text field and button are of type input in html, that's why your CSS style applied to both.
Related
I've been following the rails guide for setting up a blog. I want to have subtitles in my post and I can't figure out how to add subtitles using a form. Is there a helper for this and if not how/where do I define my own?
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
I dont have enough reputation to comment, but according to your comment, you get an undefined method error for 'sub_title', but in your migration you have named the column 'sub_title_1'.
It would appear this is a naming issue.
Either change your migration file to create a column called 'sub_title' and not 'sub_title_1', or in your form, use 'f.text_field :sub_title_1' instead of 'f.text_field :sub_title'
I'm currently following a Ruby on Rails tutorial book and I've noticed that the newer version of Rails is quite a bit different. A couple of given commands were different than described in the book and I've had to look up a few ways on how to fix these things. Right now though, I don't know what's going wrong. I have created a database table products and I'm simply trying to use a form to display some input components etc to create a new product. The book told me to do this:
<h1>New product</h1>
<% form_for(#product) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :title %><br />
<%= f.text_field :title %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description, :rows => 6 %>
</p>
<p>
<%= f.label :image_url %><br />
<%= f.text_field :image_url %>
</p>
<p>
<%= f.label :price %><br />
<%= f.text_field :price %>
</p>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<%= link_to 'Back', products_path %>
However, the view only shows me the New product header and the link to go back. I've already installed a different gem because the f.error_messages apparently wasn't used anymore either. The problem is, the entire form_for part does not show up anything. Can anybody tell me how I am supposed to change this code to get it to show up on the view for creating a new product?
Thanks!
This is what it shows:
You are missing = here <% form_for(#product) do |f| %>. It should be
<%= form_for(#product) do |f| %>
In my Rails 3.2 project, I have a form to create a new post in new.html.erb in app/views/posts/
<%= form_for(#post) do |post_form| %>
...
<div class="field">
<%= post_form.label :title %><br />
<%= post_form.text_field :title %>
</div>
...
<div class="actions">
<%= post_form.submit %>
</div>
<% end %>
I want to display the title label as TITLE, so I changed the code to post_form.label :TITLE %>, but it still displays Title. How can I display it as TITLE?
Since this is a matter of presentation on screen as opposed to content, rather than do the transformation in eruby code, this really ought to be done in CSS:
.field label {
text-transform: uppercase;
}
If you only want to modify the :title input's label rather than all labels having the same class, add a different class or use the input's id in the CSS rule.
If you insist on up-casing it in Rails, use the second parameter to .label:
<%= post_form.label :title, :title.to_s.upcase %>
You can modify the form label as follows:
<%= post_form.label(:title, "TITLE") %>
Source: http://guides.rubyonrails.org/form_helpers.html
I have to create a form which includes Batches and Samples.
General idea would be Batches having (received:date, group_leader:string, contact_person:string, batch_comment:string and num_of_samples:integer) .
N.B batches have many samples.
now the Samples include ( sample_name, species, sample_type, ini_conc, ini_vol, and sample_comment).
Now as these are of one to many relationships, I have created a form after following many tutorials having a page showing batch detail entry as well as asking for how many samples it involved in each batch. After the user types in (for eg. 3), the form generates three fields having sample details (also including name, species, type, conc, vol, etc.) but I was not able to send these details to the database.
The only thing that updates the database is one sample information.
my view looks something like this :
New sample:
<p><% form_for(sample) do |f| %></p>
<p><% (1..#batch.sample_no).each do |i| -%></p>
<%= f.error_messages %>
<p>
<%= f.hidden_field :batch_id %>
</p>
<p>
<%= f.label :sample_name %><br />
<%= f.text_field :sname %>
</p>
<p>
<%= f.label :organism %><br />
<%= f.text_field :organism, :rows => 2 %>
</p>
<p>
<%= f.label :sample_type %><br />
<%= f.select(:samp_type, %w{ DNA RNA}) %>
</p>
<p>
<%= f.label :ini_conc %><br />
<%= f.text_field :ini_conc %>
</p>
<p>
<%= f.label :ini_vol %><br />
<%= f.text_field :ini_vol %>
</p>
<p>
<%= f.label :storage_space %><br />
<%= f.text_field :storage_space %>
</p>
<p>
<%= f.label :samp_comment %><br />
<%= f.text_area :samp_comment, :rows => 3 %>
</p>
<% end -%>
<p><%= f.submit 'Submit' %></p>
<% end %>
I haven't done anything much with my controller. (in fact I have no idea which controller I should add codes to) meaning I have created two scaffolds, one for batch and one for samples and I have made a partial of the samples_view_new.html.erb and saved it in the batch_view folder which gives me an opportunity to add samples in the show_html.erb of the batch.
my models of batch.rb and sample.rb look some thing like this
<p>class Batch < ActiveRecord::Base</p>
<p>has_many :samples</p>
<p>end</p>
<p>class Sample < ActiveRecord::Base </p>
<p>belongs_to :batch</p>
<p>end</p>
if I understand your requirements correctly, you're searching for nested forms as excellently described by Ryan Bates at railscasts.com
Tutorial 1: Nested Model Form Part 1
Tutorial 2: Nested Model Form Part 2
Here is some more help for you:
In your Bash model
has_many :samples
accepts_nested_attributes_for :samples
Your Bash Controller will have at least 2 actions: new and create
In your new action:
#bash = Bash.build
4.times { #bash.samples.build } # where 4 is the no. of samples, could also be param[:amount] in case you add a preliminary action with a form and an amount field, but I suggest using javascript as described in Tutorial 2 above for better user experience
In the create action:
#bash = Bash.new(params[:bash]) # you now have access to #bash.samples
if #bash.save ...
And finally the "new" view:
<%= form_for #bash do |f| %>
<%= f.text_field :group_leader %> <!-- ... -->
<%= f.fields_for :samples do |builder| %>
<%= builder.text_field :sample_name %> <!-- ... -->
<% end %>
<%= end %>
With this and the tutorials you should now be able to build a killer app for your degree's application. Good luck!
I think I have the solution to the problem...
My main idea was to send multiple rows in the database at once... so just follow this tutorial
http://www.stephenchu.com/2008/03/paramsfu-3-using-fieldsfor-and-index.html
My example form
<% form_for #ad do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :ad_type_id %><br />
<%= f.collection_select(:ad_type_id, AdType.all, :id, :name) %>
</p>
<p>
<% #ad.ad_properties.each do |property| %>
<%= property.name %>:
<% f.fields_for :ad_values do |value_field| %>
<%= value_field.text_field :ad_id, :value => #ad.id %>
<%= value_field.text_field :ad_property_id, :value => property.id %>
<%= value_field.text_field :value %>
<% end %><br /><br />
<% end %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_area :description %>
</p>
<p><%= f.submit %></p>
<% end %>
Explanation:
Ad has many properties. I can add new properties at any time (it's a normal model).
Lets say the Ad is of the type 'hotel'. Then I would add properties like 'stars' and 'breakfast_included'
Then I store each of these properties' values in a separate model.
And all this works fine with my form above.
My problem:
These fields are not validated because I can't know what their names are.
I need to add validations dynamically somehow.
My thought:
#Before the normal validations kick in
def add_validations
self.properties.each do |property|
property.add_validation :whatever #somehow :)
end
end
How could I do this?
Have you tried with polymorphic associations ? That's maybe a cleaner approach.
http://railscasts.com/episodes/154-polymorphic-association
Disclaimer: I've never done this before, so I'm not 100% sure it will work.
But as long as you can get the type of the object you are working with, you can use the Rails constantize method to get the model it references (I'm assuming that if ad can be of type Hotel, then you have a Hotel model). At that point you should probably have the appropriate validations on your Hotel model.