Basic Ruby On Rails Linking Help - ruby-on-rails

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.

Related

Form not showing up - 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| %>

Rails form_for in function of another value

I'm trying to use the form_for helper in order to create a form that has a variable number of text fields.
on one page, I have a form that takes in two values :
<%= form_for :specs, url: specs_path do |f| %>
<p>
<%= f.label :title %><br>
<%= f.text_field :title %>
</p>
<p>
<%= f.label :sections %><br>
<%= f.text_field :sections %>
</p>
<p>
<%= f.submit %>
</p>`
<% end %>
This take me to another page where I would like to have a form that would have a variable number of text fields. The number of fields would be = to <%= #specs.sections %>
I haven't added a regex yet ;) (I'll do it once I'm sure this is the best way)
if <%= #specs.sections %> is two, i'd want to have a form that looks like :
<%= form_for :sections, url: sections_path do |f| %>
<p>
<%= f.label :head_chord %><br>
<%= f.text_field :head_chord %>
</p>
<p>
<%= f.label :section_1_chord %><br>
<%= f.text_field :section_1_chord %>
</p>
<p>
<%= f.label :section_2_chord %><br>
<%= f.text_field :section_2_chord %>
</p>
<p>
<%= f.label :foot_chord %>
<%= f.text_field :foot_chord%>
</p>
<p>
<%= f.label :camber %>
<%= f.text_field :camber %>
</p>
<p>
<%= f.label :draft_position %>
<%= f.text_field :draft_position %>
</p>
<p>
<%= f.submit %>
</p>
<% end %>
</p>
(section is the only variable aspect.)
I appreciate any suggestion!
Thanks for reading :D
If you want to pass a variable number of parameters to your server, you'd better put them in an array. This way your controller can handle them more easily. So instead of section_1_chord, section_1_chord, ..., go with section_chord[]. You can do that this way:
<%= form_for :sections, url: sections_path do |f| %> <p>
<p>
<%= f.label :head_chord %><br>
<%= f.text_field :head_chord %>
</p>
<%= #specs.sections.each_with_index do |section, index| %>
<p>
<%= label_tag "section_chord-#{index}", "section #{index}"%><br>
<%= text_field_tag 'section_chord[]', nil, id: "section-chord-#{index}" %>
</p>
<% end %>
This way rails treats section_chord as an array. This code is untested an will most likely need some clean up, but it should work.

if-else statements in the view

<% form_for :customer, :url => {:action => :save_order} do |f| %>
<%= f.error_messages %>
<%= f.label :"Select Payment Method" %>
<%= f.select :payment_method, [['Cash','cash'],['Credit Card','credit card']] %>
<p>
<%= f.label :customer_first_name %><br />
<%= f.text_field :first_name %>
</p>
<p>
<%= f.label :customer_last_name %><br />
<%= f.text_field :last_name %>
</p>
<p>
<%= f.label :email_address %><br />
<%= f.text_field :email %>
</p>
<% if #customer.payment_method == "Credit Card" %>
<p>
<%= f.label :credit_card %><br />
<%= f.text_field :credit_card_number %>
</p>
<% else %>
<% end %>
<%= f.submit "Place Order"%>
Here's my code. I just want the credit card number field to appear when the user selects credit card as their payment method. But when i select credit card in the drop down menu nothing happens. Thanks for your help!
Your if statement will run server side, but you need client side logic to respond to the change in the drop-down list. You could show the credit card 'p' regardless, but hide it initially via CSS, then have some jQuery similar to this -
$("payment_method").change(function () {
if ($(this).val() == "credit card") $(".creditcard").show();
})
You need to use Javascript to handle this problem. What you are trying is not working because the code is parsed server-side once.
Here is a Railscast which is dealing with a simmilar Problem:
http://railscasts.com/episodes/88-dynamic-select-menus
You can transfer the techniques to your problem.

Show or hide fields depending on the Acl9 role - Ruby on Rails

I am using Acl9 to manage the roles and I want to hide the checkbox usertype if the user has the role :customer and show it if the role is :manager. I want that just the :manager can edit all the fields and some for the :customer.
Thank you for your help!
<h1>Editing user</h1>
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :usertype %><br />
<%= f.check_box :usertype %>
</p>
<p>
<%= f.label :surname %><br />
<%= f.text_field :surname %>
</p>
<p>
<%= f.label :firstname %><br />
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :registrationdate %><br />
<%= f.datetime_select :registrationdate %>
</p>
<p>
<%= f.label :login %><br />
<%= f.text_field :login %>
</p>
<p>
<%= f.label :password %><br />
<%= f.text_field :password %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', #user %>
<%= link_to 'Back', users_path %>
You can also do think like that:
..in your application_helper.rb
module ApplicationHelper
include Acl9Helpers
end
..and in your views, something like that
<% show_to(:admin) do %>
The content to show
<% end %>
According to the Acl9 documentation, you should be able to do something like this:
<% if #user.has_role?(:manager, nil) %>
<p>
<%= f.label :usertype %><br />
<%= f.check_box :usertype %>
</p>
<% end %>

DRY form partial for create and update

I have a _form.html.erb form partial which helps to DRY up my code but I need the form to have different labels depending on if I am creating a new user or updating an existing user.
Here is my form partial. I don't need to show the eula checkbox during update and I also need to replace the "Create my account" submit button text to something more appropriate when doing an update.
<% form_for #user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, 'Full name' %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, 'Email address' %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<p>
<%= f.check_box :eula %>
<%= f.label :eula, 'I agree to the terms and conditions' %>
</p>
<p><%= f.submit "Create my account" %></p>
<% end %>
Which one of the following is the best way to do this?
have 2 separate form partials, one for create and one for update
have 1 form partial but have conditional labels based on the action (is this possible?)
factor the common part into a partial and reuse that in the create and update forms
If I were to do conditional form how would I check which action is being performed?
ActiveRecord has the new_record? method which you can use to decide what to show on the form:
<% form_for #user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, 'Full name' %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, 'Email address' %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</p>
<% if #user.new_record? %>
<p>
<%= f.check_box :eula %>
<%= f.label :eula, 'I agree to the terms and conditions' %>
</p>
<% end %>
<p><%= f.submit #user.new_record? ? "Create my account" : "Update my account" %></p>
<% end %>
Wrap the <form> tag around the call partial tag and put the submit button in the respective views. Only put the eula check box in the create view.
You can create a variable in the new and update views and use that as your label name.
<%= f.label email, emaillabel %>
[Edit]
If you need to pass variables to a partial use this:
<%= render :partial => 'form', :locals => { :myvar => myvar } %>

Resources