Form not showing up - Ruby on Rails - ruby-on-rails

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

Related

creating a dropdown list in a form_for helper referring to another model

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.

Rails form_for added empty model

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.

Active Admin, one page - two diffrent forms

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.

Basic Ruby On Rails Linking Help

So I am beginning to work with Rails and I get some of the concepts but am stuck on an important one.
Let's say I have customers which has many jobs and jobs which belongs to customers.
How would I go about creating a new job for a customer?
I can create a link that goes to customers/1/jobs/new and I can grab the customer ID but how do I tell it that I am creating a job for customer 1?
I know this is the most basic of things but I just need a push in the right direction.
This is my form so far:
How do I get :customer_id to populate with the customer_id param?
<h1>New job</h1>
<% form_for(#job) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :customer_id %><br />
<%= f.text_field :customer_id %>
</p>
<p>
<%= f.label :manufacturer %><br />
<%= f.text_field :manufacturer %>
</p>
<p>
<%= f.label :serial_number %><br />
<%= f.text_field :serial_number %>
</p>
<p>
<%= f.label :problem %><br />
<%= f.text_area :problem %>
</p>
<p>
<%= f.label :notes %><br />
<%= f.text_area :notes %>
</p>
<p>
<%= f.label :status %><br />
<%= f.text_field :status %>
</p>
<p>
<%= f.label :tech_id %><br />
<%= f.text_field :tech_id %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', jobs_path %>
Just use form_for([#customer, #job]), this should generate the correct URLs (/customers/:customer_id/jobs etc).
You can then fetch params[:customer_id] in your JobsController.create method.
Here's a long, maintained, descriptive list of good ruby programming tutorials. Good luck.

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?
If I take this form:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
And alter it to improve the clarity of what's expected in the field:
<% form_for(#thing) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label "What would you like to call your thing?" %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
The for attribute on the label tag would read "thing_What would you like to call your thing?", which obviously destroys its relationship with the intended partner field.
So how do I alter the label text while preserving that relationship?
<%= f.label :name, "What would you like to call your thing?" %>
See label’s documentation (and on APIdock).

Resources