Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Html 尝试在应用程序布局中加载视图并在每个页面上调用其操作_Html_Ruby On Rails_Model View Controller_Action_Bootstrapping - Fatal编程技术网

Html 尝试在应用程序布局中加载视图并在每个页面上调用其操作

Html 尝试在应用程序布局中加载视图并在每个页面上调用其操作,html,ruby-on-rails,model-view-controller,action,bootstrapping,Html,Ruby On Rails,Model View Controller,Action,Bootstrapping,我正在尝试加载带有产品类型列表的视图。我需要在每个页面上显示视图。但是在其他页面中没有要求采取行动,请告诉我哪里出了问题,并提出任何替代方案 index.html.erb 每当我跳转到另一页时,不会调用该操作。 请建议备选方案。/产量的内容\u仅用于当前操作调用的当前视图。因此,如果其他页面的视图中没有:侧栏的内容,那么它们就不会有侧栏。如果您只是直接将包含在内容中,它仍然不会执行所需的任何额外控制器逻辑 如果您想要可恢复的内容,而不是可重复使用的“空间”,请使用帮助器或类似于布局/标题的部分

我正在尝试加载带有产品类型列表的视图。我需要在每个页面上显示视图。但是在其他页面中没有要求采取行动,请告诉我哪里出了问题,并提出任何替代方案

index.html.erb 每当我跳转到另一页时,不会调用该操作。
请建议备选方案。

/
产量的内容\u仅用于当前操作调用的当前视图。因此,如果其他页面的视图中没有:侧栏的
内容,那么它们就不会有侧栏。如果您只是直接将
包含在
内容中,它仍然不会执行所需的任何额外控制器逻辑

如果您想要可恢复的内容,而不是可重复使用的“空间”,请使用帮助器或类似于
布局/标题的部分

对于您不希望只在分部中执行的操作,而且不仅在direct Ruby中(使用
content\u 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/\u products\u sidebar.html.erb
(我肯定会考虑其他模板引擎)


然后在索引中,您可以调用它,它将不依赖于当前正在处理的操作/控制器

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


收益率:侧栏
-这是如何工作的?是的,它的收益率:侧边栏,我在复制时犯了一个错误。但在我的项目中,它的指定是正确的。你需要在每个页面上都有那个侧边栏吗?它将仅出现在您显式调用侧边栏的
内容的页面上。我认为您应该创建一个
\u sidebar.html.erb
部分,您可以在
application.html.erb
中进行渲染,而不会产生任何效果。或者可能我错过了这个问题。好的,谢谢,我会尝试一下,让你知道。顺便问一下,本地人:{products:products}做什么@Fire Lancer因为您不想污染视图/控制器之外的东西的“全局”属性名称空间(例如,您可能已经在其他产品页面上有一个
@products
),所以它会将其作为局部变量传递到partial中(
products
,而不是
@products
locals
就像一个“变量名->值”散列。哦,多亏了你,它的工作方式和我想要的一样。谢谢你的帮助和解释@Fire Lancer有所有不同的选项,值得一看(以及所有内置的帮助程序,但没有任何对本案例有用)
    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
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
<div id="products_sidebar">
    <% products.each do |product| %>
        <div><%=product.name%></div> <!--whatever you want it to look like-->
    <% end %>
</div>
<body>
    <aside><%= products_sidebar %></aside>