Home>
I'm making my own web application, but I can't set the new action as I think.

List display (index action)

New form (new action)

Click Create New (assuming you clicked Create New Cell on the right next to the top 646)

Describe the content in the form and submit the form

The newly created data will be in the cell to the right of 646

I want to use

but it doesn't work.

main_cows_controller.rb

class MainCowsController</pre>
<p>index.html.erb</p>
<pre><code data-language = "HTML"><% @page_title = "Milk management"%>
<% = @page_title%>

<% if @ maincow.present?%>
  <table class = "index" align = "left">
    <% 20.downto (1) .to_a.each do | i |%>
    <% @cow = @ maincow.find_by line: i%>
     <% if @ cow.present?%>
     <tr><td><% = link_to @ cow.number, @cow%></td></tr>
     <% else%>
     <tr><td><% = link_to "Create new", new_main_cow_path%></td></tr>
     <% end%>
   <% end%>
  </table>
  <table class = "index" align = "left">
      <% 40.downto (21) .to_a.each do | i |%>
    <% @cow = @ maincow.find_by line: i%>
     <% if @ cow.present?%>
     <tr><td><% = link_to @ cow.number, @cow%></td></tr>
     <% else%>
     <tr><td><% = link_to "Create new", new_main_cow_path%></td></tr>
     <% end%>
   <% end%>
  </table>
<% end%>

new.html.erb

<% @page_title = "Create new"%>
<% = @page_title%>
<% = form_for @maincow do | form |%>
  <% = render "form", form: form%><% = form.submit%>
<% end%>

_form.html.erb

<table class = "form">
   <tr>
     <th><% = form.label: number, "Individual identification number"%></th>
     <td><% = form.text_field: number, size: 8%></td>
   </tr>
   <tr>
     <th><% = form.label: name, "Name"%></th>
     <td><% = form.text_field: name, size: 50%></td>
   </tr>
   <tr>
     <th><% = form.label: birthday, "date of birth",
             for: "maincow_birthday_1i"%></th></th>
     <td><% = form.date_select: birthday, start_year: 2010, end_year: Time.current.year,
             use_month_numbers: true%></td>
   </tr>
</table>

Migration script

class CreateMainCows

Migration script # 2

class AlterMainCows

I think that the problem itself can be solved if you can enter a line column in the form.
After you click New, is set line column of the clicked cell is in the auto, in the form I want to to enter number column, name column, the only birthday column.
I think what a good way?

  • Answer # 1

    How to identify cows is a mess.
    If you look at migration, id, number, and line are likely to hit it. However, it is better not to use it for that purpose because it is not unique to cattle. For example, when you become 100 heads and display 20 heads per screen, it is troublesome.
    If managed by number,<% @cow = @ maincow.find_by line: i%>and not fetching from DB in view
    Use@maincow = MainCow.order ("number")in controller index

    <% 20.downto (1) .to_a.each do | i |%>
        <% @cow = @ maincow.find_by line: i%>


    <% @ maincow.each do | cow |%>
         <% = link_to cow.number, cow%>
         <% end%>
         <% = link_to "Create new", new_main_cow_path%>


    However, since there is no one at the beginning, the top 20 lines are empty and only the last new creation is made.