在html中使用ruby注入时出现奇怪的语法错误

时间:2015-12-19 12:21:14

标签: html ruby-on-rails ruby ruby-on-rails-3 code-injection

我得到的错误是:

PagesController#main中的SyntaxError

  

C:/Users/Sarah/RubymineProjects/MyApp/app/views/calendars/_show.html.erb:49:   语法错误,意外')',期待keyword_then或';'或'\ n'   ... =(如果event.starts_at.monday?); @ output_buffer.safe_append ='... ^   C:/Users/Sarah/RubymineProjects/MyApp/app/views/calendars/_show.html.erb:52:   语法错误,意外的keyword_end,期待')''。freeze;结束^   C:/Users/Sarah/RubymineProjects/MyApp/app/views/calendars/_show.html.erb:54:   语法错误,意外的keyword_end,期待')''。freeze;结束^   C:/Users/Sarah/RubymineProjects/MyApp/app/views/calendars/_show.html.erb:364:   语法错误,意外的keyword_ensure,期待')'   C:/Users/Sarah/RubymineProjects/MyApp/app/views/calendars/_show.html.erb:366:   语法错误,意外的keyword_end,期待')'

但我正在尝试编写的代码是:

<% @events.each do |event|%>
        <%= if event.starts_at.monday? %>

            <p><%= event.name %></p>
            <% end %>

        <% end %>

这真的是因为我的语法(我认为似乎没问题)或者我的代码在哪里出现了问题? 我在_show.html.erb(来自日历控制器)中编写此代码,这是在我的main.html.erb中呈现的部分(来自Pages控制器)

这是我用来渲染我的部分的代码:

<%= render partial: '/calendars/show', locals: {} %>

这是我的控制器和型号:

class PagesController < ApplicationController

  #before_action :require_customer, only: [:main]

  def home

  end


  def main
    @events = current_customer.calendar.events
  end

end

我的日历控制器:

class CalendarsController < ApplicationController

  def new
    @calendar = Calendar.new(calendar_params)

  end

  def create
    @calendar = Calendar.new(calendar_params)
  end

private
  def calendar_params
    params.require(:customer_id)
  end


  def show
    @calendar = current_customer.calendar
    @events = @calendar.events

  end

end

我的事件控制器(仅供参考)

class EventsController < ApplicationController
  before_filter :logged_in?


  def new
    @event = Event.new
    @calendar = current_customer.calendar

  end


  def create

    @calendar = current_customer.calendar

    @event = @calendar.events.build(event_params)

    if @event.save
      redirect_to '/main' #'/main/#{@calendar.id}'
    else
      redirect_to '/compose'
      end

  end

private
  def event_params
    params.require(:event).permit(:calendar_id, :name, :starts_at, :ends_at) 
  end

这里也是我的日历和活动模型:

class Calendar < ActiveRecord::Base
  belongs_to :customer
  has_many :events
end


class Event < ActiveRecord::Base
  belongs_to :calendar

  end

我已经被困在这几天,我似乎无法找到解释。 谢谢!

1 个答案:

答案 0 :(得分:0)

关注Simone answer我希望添加额外的信息。因为Ruby是更具表现力的语言。我想重构一下剪掉的代码。

<% @events.each do |event|%>
    <p><%= event.name if event.starts_at.monday? %></p>
<% end %>