Previous post of this article :-
<ul> <li><a href="https://utopian.io/utopian-io/@meblogger/introduction-to-ruby-on-rails-and-an-employee-managing-application-issue1" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Introduction to Ruby on Rails and An Employee Managing Application - #issue1 <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1515254821/nhptw0lgjb4msye8zhgs.jpg" alt="rails.jpg" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1515254821/nhptw0lgjb4msye8zhgs.jpg 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1515254821/nhptw0lgjb4msye8zhgs.jpg 2x" /><br /> <a href="https://sdtimes.com/api/rails-5-0-released-action-cable-api-mode-improvements/" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Image Source <hr /> <h3><center>5 .Changing views <hr /> <p dir="auto">We now want to change the display of one of the pages, for example that listing the collaborators.<br /> Rails has generated .rhml files which are html pages with ruby code in the app / views directory (part view of the MVC).<br /> Let's modify the app / views / collaborator / list.rhtml file so that the list only shows the name and surname of the collaborator and that last name + first name is a link to the complete card of the collaborator: <pre><code><h1> Contributor Listing </ h1> <Table> <% for collaborator in @collaborators%> <Tr> <Td> <% = link_to collaborator.name + '' + collaborator.prenom, : action => 'show', : id => collaborator%> </ Td> <Td> <% = link_to 'Edit', : action => 'edit', : id => collaborator%> </ Td> <Td> <% = link_to 'Destroy', {: action => 'destroy',: id => collaborator}, : confirm => 'Are you sure?', : post => true%> </ Td> </ Tr> <% end%> </ Table> <% = link_to 'Previous page', {: page => @ collaborator_pages.current.previous} if @ collaborator_pages.current.previous%> <% = link_to 'Next page', {: page => @ collaborator_pages.current.next} if @ collaborator_pages.current.next%> <br /> <% = link_to 'New collaborator',: action => 'new'%> <p dir="auto">For the date field of our form to be more <code>"francophone", the file must be modified <code>app / views / collaborator / _form.rhtml and put more options on the <code>date_select: <pre><code><% = date_select 'collaborater', 'date of birth', : order => [: day,: month,: year], : use_month_numbers => true, : start_year => 1900, : end_year => Date.today.year, : include_blank => true%> <p dir="auto">This statement generates a <code>SELECT field in our html form, the arguments allow us to configure at our convenience. the <code>selected_date fetches the birth date information from the collaborator table. <p dir="auto">The order of the select will be (day, month, year), the names of the months will be replaced by their numbers,the range of years will be [1900, current year] and the user has the option of not filling in any of the select ones. <p dir="auto">To have the date display format defaults to <code>dd / mm / yyyy and the names of the days and months are in French, you must add at the end of the file <code>config / environment.rb this line: <p dir="auto"><center><code>require 'overrides' <p dir="auto">and add the file lib / overrides.rb: <pre><code>Date :: MONTHS = { 'January' => 1, 'February' => 2, 'Mars' => 3, 'April' => 4, 'Mai' => 5, 'June' => 6, 'July' => 7, 'August' => 8, 'September' => 9, 'October' => 10, 'November' => 11, 'December' => 12} Date :: DAYS = { 'Monday' => 0, 'Tuesday' => 1, 'Wednesday' => 2, 'Thursday' => 3, 'Friday' => 4, 'Saturday' => 5, 'Sunday' => 6} Date :: ABBR_MONTHS = { 'jan' => 1, 'fev' => 2, 'mar' => 3, 'avr' => 4, 'may' => 5, 'june' => 6, 'Jul' => 7, 'aou' => 8, 'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12} Date :: ABBR_DAYS = { 'lun' => 0, 'mar' => 1, 'sea' => 2, 'game' => 3, 'ven' => 4, 'sam' => 5, 'dim' => 6} Date :: MONTHNAMES = [nil] +% w (January February March April May June July August September October November December ) Date :: DAYNAMES =% w (Monday Tuesday Wednesday Thursday Friday Saturday Sunday) Date :: ABBR_MONTHNAMES = [nil] +% w (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) Date :: ABBR_DAYNAMES =% w (Mon Tue Wed Thu Fri Sat Sun) class Time alias: strftime_nolocale: strftime def strftime (format) format = format.dup format.gsub! (/% a /, Date :: ABBR_DAYNAMES [self.wday]) format.gsub! (/% A /, Date :: DAYNAMES [self.wday]) format.gsub! (/% b /, Date :: ABBR_MONTHNAMES [self.mon]) format.gsub! (/% B /, Date :: MONTHNAMES [self.mon]) self.strftime_nolocale (format) end end <hr /> <h3><center>6 . Mapping Relationship 1-n <hr /> <p dir="auto">We will add a functions table to our database to illustrate 1-n relationships.<br /> An employee has a function and a function can be exercised by several employees. <pre><code>CREATE TABLE fonctions ( id int(11) NOT NULL auto_increment, titre varchar(255) NOT NULL, PRIMARY KEY (id) ) <p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1515257387/aquhsbsys0yshnbsvlof.png" alt="rub4.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1515257387/aquhsbsys0yshnbsvlof.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1515257387/aquhsbsys0yshnbsvlof.png 2x" /> <p dir="auto">As for collaborators, we must have an id column as the primary key and auto-increment.<br /> In addition, we must add a <code>function_id column to the collaborators table, which will be a foreign key pointing to a record of the functions table.<br /> In a console, we issue the command:<br /> <code>ruby script \ generate scaffold function<br /> A <code>CRUD is then available for functions at the URL: <code>http: // localhost: 3000 / functions.<br /> You can use it to enter functions that will be assigned to employees.<br /> To join the two model classes, add one line in each of them: <p dir="auto">In the file <code>app / models / collaborateur.rb: <pre><code>class Contributor <ActiveRecord :: Base belongs_to: function validates_presence_of: name,: firstname validates_format_of: mail,: with => /*([^@\s]+)@((???????????????? end <p dir="auto">The belongs_to method then indicates that an employee <code>"belongs" to a function, that is to say that the foreign key Collaborator class function belongs to the Functions table. <pre><code>In the file `app / models / fonction.rb`: class Fonction < ActiveRecord::Base has_many :collaborateurs end <p dir="auto">This time, we use the method has_many, because a function can <code>"own" several collaborators, it's to say that to a function, we make correspond several collaborators. <p dir="auto">It is now necessary to modify the collaborator controller to take into account the functions.<br /> The controller classes (one per SQL table) are in the app / controllers directory.<br /> So we modify the file <code>app / controllers / collaborators_controller.rb and in particular its method edit <pre><code>def edit @collaborator = Collaborator.find (params [: id]) @functions = Function.find_all end <p dir="auto"><code>Function.find_all is a method of the Function template class that returns the list of all functions.<br /> The view will use this <code>@functions attribute to populate the form's select. <p dir="auto">Logically, we must do the same in the new method:- <pre><code>def new @collaborateur = Collaborateur.new @fonctions = Fonction.find_all end <p dir="auto">Finally, you have to add a select to the <code>app / views / collaborators / _form.rhtml file:- <pre><code><P> <B> Position: </ b> <select name = "collaborator [function_id]"> <% @ functions.each do | function | %> <option value = "<% = function.id%>" <% = 'selected' if function.id == @ collaborator.function_id% >> <% = function.title%> </ Option> <% end%> </ Select> </ P> <p dir="auto">and display this new information in the file of a collaborator in the file <code>app / views / collaborators / show.rhtml:-<br /> <center><code><p> <b> Function: </ b> <% = @ collaborator.function.title%> </ p> <hr /> <h3><center>7 . Conclusion <hr /> <p dir="auto">We saw that Rails was very productive because we had a usable application very quickly.<br /> We saw that the generated application was very simple to modify to adapt it to our needs. The architecture of the application respects the <code>MVC model, very important for the application to be easily maintainable. <p dir="auto">Rails can be used to quickly build application layouts, but the framework has also shown that<br /> could be used to develop large applications (<code>examples: Basecamp, Odeo, List Apart, Scoopeo.com, etc.) <h6><code>END OF TUTORIAL........... <h3><center> Thanks For Reading <p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@meblogger/introduction-to-ruby-on-rails-and-an-employee-managing-application-issue2" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @meblogger I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x