2016-12-09 5 views
0

私はRuby環境でRubyでコーディングすることを学んでいます。私は車と製品の2つのモデルを作った。私は車と製品へのリンクを持つメインページを持っていたい。それぞれをクリックすると、自分のビューページが表示されます。以下は、車やユーザーのビューページは、それぞれ以下のとおりです。ruby​​ on railsでのビュー

アプリ/ビュー/車/ index.html.erb

<p id="notice"><%= notice %></p> 

<h1>Listing Cars</h1> 

<table> 
    <thead> 
    <tr> 
     <th>Make</th> 
     <th>Color</th> 
     <th>Year</th> 
     <th colspan="24"></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @cars.each do |car| %> 
     <tr> 
     <td><%= car.make %></td> 
     <td><%= car.color %></td> 
     <td><%= car.year %></td> 
     <td><%= link_to 'Show', car %></td> 
     <td><%= link_to 'Edit', edit_car_path(car) %></td> 
     <td><%= link_to 'Destroy', car, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

<%= link_to 'New Car', new_car_path %> 

<h4>Import that data!</h4> 
    <%= form_tag import_users_path, multipart: true do %> 
    <%= file_field_tag :file %> 
    <%= submit_tag "Import CSV" %> 
    <% end %> 
</div> 

アプリ/ビュー/ユーザー/ index.html.erb

<p id="notice"><%= notice %></p> 

<h1>Listing Users</h1> 

<table> 
    <thead> 
    <tr> 
     <th>User</th> 
     <th>Steps</th> 
     <th>Distance</th> 
     <th>Minutes Exercised</th> 
     <th>Hours of Sleep</th> 
     <th>Calories Burned</th> 
     <th colspan="24"></th> 
    </tr> 
    </thead> 

    <tbody> 
    <% @users.each do |user| %> 
     <tr> 
     <td><%= user.user %></td> 
     <td><%= user.steps %></td> 
     <td><%= user.distance %></td> 
     <td><%= user.exercise %></td> 
     <td><%= user.sleep %></td> 
     <td><%= user.calories %></td> 
     <td><%= link_to 'Show', user %></td> 
     <td><%= link_to 'Edit', edit_user_path(user) %></td> 
     <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

<%= link_to 'New User', new_user_path %> 
<div> 

<h4>Import that data!</h4> 
    <%= form_tag import_users_path, multipart: true do %> 
    <%= file_field_tag :file %> 
    <%= submit_tag "Import CSV" %> 
    <% end %> 
</div> 

<%= form_tag import_users_path, multipart: true, class: 'form-inline' do %> 
    <div class="form-group"> 
    <%= link_to "Export CSV", users_path(format: "csv"), class: 'btn btn-primary' %> 
    </div> 

<% end %> 

私は単一のメインページを作成し、これらのビューへのリンクを提供する方法を知らない。一つの例が私を助けるだろう。先輩に感謝します。

+0

あなたは、例えば、静的なページを作成したいです各モデルのインデックスビューへのリンクがあるホームページ –

答えて

0

rails scaffoldを使用して、最初に生成された機能ページを見てください。

2

セイは.......

<%=link_to "cars" cars_path(car) %> 

<%=link_to "users" users_path(user) %> 

このリンクを使用すると、

<%= link_to "cars" cars_path %> 
<%= link_to "users" users_path %> 
0

はこれを試し書きあなたのhome.html.erbで自宅

という名前のメインページがあるとしコントローラ(ユーザー/車)の方法を表示するためにあなたを送信します。 views/cars/show.html.erbviews/users/show.html.erbにショーページを作成する必要があります。その後、自分のビュー/ショーページを表示することができます。

これがうまくいくと思います。

+0

おかげでそれは働いた!!!! –

0

routesの概念を理解する必要があります。

プロジェクトフォルダにコマンドrake routesを実行します。このようなマッピングが表示されます。

  Prefix Verb URI Pattern       Controller#Action 
     companies GET /companies(.:format)     companies#index 

したがって、ルートに従って、会社のコントローラのインデックスアクションにつながります。あなたが観察することができたよう

<%= link_to 'Companies', companies_path%> 

は、companies_pathで、companiesはルートに示す接頭辞です。

具体的なケースでは、次のリンクが機能します。あなたの home.html.erbで

<%=link_to "All Cars", cars_path%> 
<%=link_to "All Users", users_path%> 

は、 "すべてのユーザー" とusers_pathの間にカンマがあることを忘れないでください。 link_toは複数の引数を持つ単なるメソッドなので、詳細情報については

http://apidock.com/rails/v4.0.2/ActionView/Helpers/UrlHelper/link_to