【问题标题】:Why is this Ruby on Rails program giving me a syntax error? [closed]为什么这个 Ruby on Rails 程序给我一个语法错误? [关闭]
【发布时间】:2021-04-21 17:55:19
【问题描述】:

这里是 RoR 初学者。我正在尝试根据我的一个观点制作类似 instagram 的提要。我正在运行此代码:

<h2>University Student Food Recipes</h2>

<div class="container">
    <div class="row">
    <div class="col-8">
        <% @images.each do |image| %>
            <%= image_tag image.image, class: "img img-fluid" %>
    </div>
    </div>
</div>

这里是控制器:

class PagesController < ApplicationController
    def home
        @images = Image.all
    end
end

但是,页面给了我一个语法错误:

ActionView::SyntaxErrorInTemplate in PagesController#home

Encountered a syntax error while rendering template: check <h2>University Student Food Recipes</h2> <div class="container"> <div class="row"> <div class="col-8"> <% @images.each do |image| %> <%= image_tag image.image, class: "img img-fluid" %> </div> </div> </div>

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: html ruby-on-rails ruby


    【解决方案1】:

    请记住,&lt;% ... %&gt; 标记用于在您的 .erb.html 模板中运行 Ruby 代码。上面,您刚刚忘记使用 end 关键字关闭块。应该是:

        <div class="col-8">
            <% @images.each do |image| %>
                <%= image_tag image.image, class: "img img-fluid" %>
            <% end %>
        </div>
    

    见:https://www.rubyguides.com/2016/02/ruby-procs-and-lambdas/

    【讨论】:

      猜你喜欢
      • 2021-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2017-01-10
      相关资源
      最近更新 更多