2016-12-09 1 views
0

레일 환경에서 루비를 코딩하는 법을 배우고 있습니다. 나는 두 가지 모델의 자동차와 제품을 만들었습니다. 자동차와 제품에 대한 링크가있는 메인 페이지를 갖고 싶습니다. 각각을 클릭하면 자신의보기 페이지를 표시해야합니다. 다음은 자동차와 사용자를위한 뷰 페이지는 각각 다음과 같습니다레일에서 루비보기

응용 프로그램/뷰/자동차/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