Jump to content

The ultimate community for Ruby on Rails developers.


Photo

How to connect form with models?

rails ruby-on-rails ruby-on-rails4 rails4

  • Please log in to reply
2 replies to this topic

#1 alterpub

alterpub

    Passenger

  • Members
  • 2 posts

Posted 06 May 2014 - 08:26 PM

Hi, I don't understand how to create form for my purpose.  
I have 4 models:  
 
    Territory name:string  
    Jobtype name:string  
    Server name:string login:string ip:string  
    Jobs territory:referenced jobtype:referenced server:referenced is_running:boolean 
 
also, I've 5 controllers:  Territory, Jobtype, Server, Jobs and Home  
first 4 controllers were generated with models through scaffold, the last one was generated like  
 
    rails g controller Home index
 
I want to make form in views/home/index.html.erb.  
Form have to contains <select> menu for territories(field name, but as it's reference, then value have to be territory_id), select menu for jobtype and select menu for servers.  
And finally button 'Submit' which should write in table jobs IDs of choosed options.  
 
Please, explain me how to work with different models in one controller/view


#2 Lauree

Lauree

    Passenger

  • Members
  • 6 posts
  • LocationNew York

Posted 09 May 2014 - 04:32 AM

Hello,

You can use following form structure:

  1. In app/views/home/index.html.erb
    <%= form_for @job do |f|%>
      <%= f.collection_select(:territory_id, collection_for_territory, :id, :name, prompt: true) %>
      <%= f.collection_select(:job_type_id, collection_for_job_type, :id, :name, prompt: true) %>
      <%= f.collection_select(:server_id, collection_for_server, :id, :name, prompt: true) %>
      <%= f.submit "Submit Job"%>
    <% end %>
    
  2. In app/helpers/homes_helper.rb
    def collection_for_territory
      Territory.all # Here you can apply any scopes which you need to filter record collection
    end
    
    def collection_for_job_type
      JobType.all 
    end
    
    def collection_for_server
      Server.all
    end 
    
    
    Thanks,
    Lauree
    
    

  • alterpub likes this

Hire skilled Ruby on Rails Developer

Check out Ruby on Rails Outsourcing Services by Allerin


#3 alterpub

alterpub

    Passenger

  • Members
  • 2 posts

Posted 14 May 2014 - 04:57 AM   Best Answer

Thank you, Lauree, It's clear for me now !!







Also tagged with one or more of these keywords: rails, ruby-on-rails, ruby-on-rails4, rails4

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users