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'
Related
im new to ruby on rails, im trying to create a dropdown list in a form_for helper referring to another model (users). basically, im trying to select from list of users so that their email will be copied to the receiver field.
here's what i have so far, i can only manually type in the email address:
<%= form_for :femail, url: {action: "create"} do |form| %>
<p>
<%= form.label :from %><br>
<%= form.text_field :from %>
</p>
<p>
<%= form.label :to %><br>
<%= form.text_area :to %>
</p>
<p>
<%= form.label :subject %><br>
<%= form.text_field :subject %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
thanks in advance.
You'll need to have your set of users coming from somewhere (maybe your controller?), I'm going to call it #users. Then you can do something like:
<p>
<%= form.label :to %><br>
<%= form.select(:to, options_for_select(#user.map { |u| [u.name, u.email]}) ) %>
</p>
That will show you a set of options that display the user.name, but will actually submit the value user.email. The select tag has some pretty useful options (e.g. setting default values, not accepting blank entries, etc).
More info: this section of the Rails guides and the FormOptionsHelper docs.
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| %>
I have started the "Get Started Guide" from the ruby on rails website. Everything works fine, but when I change the order of showing all comments and than display the comments-form in the other way round, than the form_forfunction adds a empty comments model to #post.comments and so, I display one empty comment in the loop.
Here is the view:
<h1><%= #post.name %></h1>
<p><%= #post.text %></p>
<h2>Add comment</h2>
<%= form_for([#post, #post.comments.build]) do |f| %>
<p>
<%= f.label :name %><br>
<%= f.text_field :name %>
</p>
<p>
<%= f.label :email %><br>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :text %><br>
<%= f.text_area :text %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
<h2>Comments</h2>
<%= render #post.comments %>
The loop display two comments. One, that exists in the db and one, that has just empty attributes. If I delete the form, than all is shown up correct.
You can select only your persisted comments:
<%= render #post.comments.select(&:persisted?) %>
When you do post.comments.build against some post, it will be added to the post.comments collection and will be displayed along with other comments.
You can always use persisted to check if the object is present in database meaning id is assigned to it.
#post.comments.select(&:persisted?)
Note: .present? check donot work here so you have to use .persisted.?
present check only assosiated to the parent.
I have following two resources:
Position
JobTitle
And a position belongs_to job title.
In page Position, I create new position, with job_title select and some other selects. I need also create new job_title in this page, instead of choose select variants. So, in fact, or I choose job title in existing base, or create new, and save it for this position.
How can I make this? I looking for answer, and reading docs, but nothing!
You need to use fields_for in your view to send params for two models at a time.
Here's how I would try to solve to problem:
<%= form_for #job_title do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :description %><br />
<%= f.text_field :description %>
</p>
<%= f.fields_for :position do |builder| %>
<p>
<%= builder.label :name %> <br />
<%= builder.text_field :name %>
</p>
<% end %>
<p><%= f.submit "Submit" %></p>
<% end %>
And then, in your controller, you need to create the association. For more information, you can check out the railscast for nested forms.
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.