restaurants
에 대한 스캐 폴딩 방법을 사용하여 레일에 간단한 레일 앱을 만들었습니다.레일즈는 컨트롤러에서 뷰로 인스턴스 변수를 어떻게 전달합니까?
restaurants_controller.rb
의 컨트롤러를 표시하고 편집하는 방법입니다.
# GET /restaurants/1
# GET /restaurants/1.json
def show
end
# GET /restaurants/1/edit
def edit
end
이 restaurants/show.html.erb
다음과 같습니다 :
<p id="notice"><%= notice %></p>
<%= image_tag @restaurant.image_url %>
<p>
<strong>Name:</strong>
<%= @restaurant.name %>
</p>
<p>
<strong>Address:</strong>
<%= @restaurant.address %>
</p>
...
및 restaurants/edit.html.erb
: 여기
<h1>Editing Restaurant</h1>
<%= render 'form', restaurant: @restaurant %>
<%= link_to 'Show', @restaurant, class: "btn btn-link" %> |
<%= link_to 'Back', restaurants_path, class: "btn btn-link" %>
내 질문은 : 그들은 단지 빈 방법이 얼마나 주목 내 현재의 이해 (잘못 될 수)입니다 인스턴스 변수 인이 경우에는
@restaurant
을
restaurant_controllers.rb
에 정의하고, Rails는 contr에 정의 된 변수를 자동으로 연결합니다 보기에 oller. 레스토랑 컨트롤러 예를 들어, 인덱스 방법 : 나는
index.html.erb
에서
@restaurants
를 호출 할 때
def index
@restaurants = Restaurant.all
end
, 레일 views
에 사용되는 인덱스 방법에서 @restaurants
가 나타납니다. 비어있는 방법은
어디 레일 restaurants_controller.rb
에도 불구하고 show
및 edit
방법에서 @restaurant
인스턴스 show.html.erb
에서 변수 edit.html.erb
나올까요입니까? 나는 5
컨트롤러에'before_action : set_restaraunt'이 있습니까? –
컨트롤러에'load_and_authorize_resource'와 함께 CanCan을 사용하고 있습니까? – David