0

제품 유형 목록이있는보기를로드하려고합니다. 모든 페이지에 표시 할보기가 필요합니다. 하지만 다른 페이지에서 조치가 취해지지 않고 있습니다. 어디서 잘못 됐는지 알려주고 다른 대안을 제안하십시오.응용 프로그램 레이아웃에서보기를로드하고 모든 페이지에서 해당 동작을 호출하려고 시도했습니다.

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 

내가 다른 페이지로 이동할 때마다 액션이 호출 받고 있지 않습니다. 대안을 제안하십시오.

+0

'yield : sidebar' - 어떻게 작동하나요? '수율 : 사이드 바'가 될까요? – Magnuss

+0

그래, 그 수율 : 사이드 바, 나는 복사하는 동안 실수를했다. 하지만 내 프로젝트에서 올바르게 지정했습니다. –

+0

모든 페이지에 사이드 바가 있어야합니까? 명시 적으로 'content_for : sidebar'를 호출하는 페이지에서만 나타납니다. 나는 당신이'_sidebar.html.erb' 부분을 만들어야한다고 믿습니다.이 부분은'application.html.erb'에서 출력하지 않고 렌더링합니다. 또는 어쩌면 나는 문제를 놓치고있다. – Magnuss

답변

1

content_for/yield은 현재 동작에 의해서만 호출됩니다. 따라서 다른 페이지에 자신의 견해에 content_for :sidebar이 없으면 사이드 바가 없을 것입니다. content_for에 직접 포함 시키면 추가 컨트롤러 로직을 실행하지 않아도됩니다.

재사용 가능한 "공간"이 아닌 재생 가능한 콘텐츠가 필요한 경우 layouts/header과 같은 도우미 또는 부분을 사용하십시오.

부분적으로 만하고 싶지는 않지만 직접 Ruby (content_tag 등)뿐만 아니라 부분적으로 도우미를 결합 할 수도 있습니다.

class ApplicationHelper # Or any other helper 
    def products_sidebar 
    products = Product.where(show_on_sidebar: true).order(:popularity) # Or whatever you like 
    render partial: "shared/products_sidebar", locals: {products: products} 
    end 
end 

shared/_products_sidebar.html.erb

<div id="products_sidebar"> 
    <% products.each do |product| %> 
     <div><%=product.name%></div> <!--whatever you want it to look like--> 
    <% end %> 
</div> 

그런 다음 색인에, 당신은 단지 그것을 호출 할 수 있습니다 (ID 확실히 다른 템플릿 엔진에 대한 고려를), 그리고 늘 현재 처리되고있는 액션/컨트롤러에 따라 달라집니다.

<body> 
    <aside><%= products_sidebar %></aside> 
+0

괜찮아, 고마워. 너 한테 알려 줄께. 그건 그렇고, 지역 주민들 : {제품 : 제품}은 무엇을합니까? . @Fire Lancer –

+0

보기/컨트롤러 (예 : 이미 다른 제품 페이지에'@ products '가있을 수 있음)에 대한 "전역"@attribute 네임 스페이스를 오염시키지 않으므로 부분적으로 전달합니다 지역 변수 ('@ products'보다는'products')가됩니다. 'locals'는 "변수 이름 -> 값"해시와 같습니다. –

+0

오, 당신 덕분에, 그 일은 내가 원했던 것처럼. 도움과 설명에 감사드립니다[email protected] 파이어 랜서 –