제품 유형 목록이있는보기를로드하려고합니다. 모든 페이지에 표시 할보기가 필요합니다. 하지만 다른 페이지에서 조치가 취해지지 않고 있습니다. 어디서 잘못 됐는지 알려주고 다른 대안을 제안하십시오.응용 프로그램 레이아웃에서보기를로드하고 모든 페이지에서 해당 동작을 호출하려고 시도했습니다.
index.html.erb은
<h1>Listing the types of products</h1>
<% content_for :sidebar do %>
<% @types.each do |type| %>
<li><%= link_to type.name, product_type_path(type)%> <!-- go to product_type controller show action-->
<%= link_to "Edit", edit_product_type_path(type) %> <!-- go to product_type controller edit action -->
<%= link_to "Delete", product_type_path(type) , method: :delete, data: { confirm: "Are U Sure?" }%> <!-- go to product_type controller delete action and a pop to confirm the action -->
</li>
<% end %>
<h3><%= link_to "New Type Of Product", new_product_type_path %></h3>
<h3><%= link_to "All Types of Products", products_path %></h3> <!-- All types of products listed for admin's ease -->
<% end %>
이것은 내가 사용하고 응용 프로그램 레이아웃입니다.
Application.html.erb
<!DOCTYPE html>
<html>
<head>
<%= render 'layouts/title' %>
<%= render 'layouts/rails_defaults'%>
<%= render 'layouts/shim' %>
</head>
<body>
<aside><%= yield: sidebar %></aside>
<%= render 'layouts/header' %>
<%= render 'layouts/flash' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
내가 수율 사용하고 볼 수 있듯이 : 사이드 바 있지만 내가 다른 페이지로 이동하면 의미에서 동작 지수가 호출 점점되지 않으며, 제대로 작동하지 .
Product_types 컨트롤러
클래스 ProductTypesController <와 ApplicationController
def index
@types = ProductType.all #to get all the records to an instance variable(array in the case) lasts only until the scope lasts. Since the is defined in index, it lasts only till you are using the index view
end
def new
@type = ProductType.new #to create a new record into the respective model
end
def show
@type = ProductType.find(params[:id]) #Finding the type of product click on
@products = Product.where(value: @type.value) #finding all the products whose value field is same as the type of product value(primary key)
end
def create
@type = ProductType.new(type_params) #type params defined below in private class
if @type.save #save the product created
redirect_to root_url #and redirect to root
else #if doesnt save and error occurs
render 'new' #error occurs and render 'new' view
end
end
def edit
@type = ProductType.find(params[:id])
end
def update
@type = ProductType.find(params[:id])
if @type.update(type_params) #update the params
redirect_to root_url #if updated redirect to root
else
render 'edit' #else if error occurs render 'edit' view again
end
end
def destroy
ProductType.find(params[:id]).destroy #destroy the record
redirect_to root_url #redirect to root
end
private
def type_params
params.require(:product_type).permit(:name,:value) #used to only permit type and value thorugh request(to prevent hacking) used in create and update action above
end
end
내가 다른 페이지로 이동할 때마다 액션이 호출 받고 있지 않습니다. 대안을 제안하십시오.
'yield : sidebar' - 어떻게 작동하나요? '수율 : 사이드 바'가 될까요? – Magnuss
그래, 그 수율 : 사이드 바, 나는 복사하는 동안 실수를했다. 하지만 내 프로젝트에서 올바르게 지정했습니다. –
모든 페이지에 사이드 바가 있어야합니까? 명시 적으로 'content_for : sidebar'를 호출하는 페이지에서만 나타납니다. 나는 당신이'_sidebar.html.erb' 부분을 만들어야한다고 믿습니다.이 부분은'application.html.erb'에서 출력하지 않고 렌더링합니다. 또는 어쩌면 나는 문제를 놓치고있다. – Magnuss