【问题标题】:No method error, undefined method for nil:NillClass没有方法错误,nil:NillClass 的未定义方法
【发布时间】:2014-11-13 12:39:05
【问题描述】:

想找到一种在日历中显示 Klass.name、Programme.name、Topics.name 和 Timeslots.starts_at 的方法。目前只设法显示“主题”而不是 topic.name(见链接)和 Timeslots.starts_at。尝试了其他方法,但不断收到无方法错误。感谢有关如何解决此问题的建议。

http://imgur.com/EPuIK0s

这是日历的视图。

app/views/timeslots/month.html.erb/_month_views.html.erb

<div id="timeslots">
  <h2 id="month">
    <%= link_to "<", date: @date.prev_month %>
    <%= @date.strftime("%B %Y") %>
    <%= link_to ">", date: @date.next_month %>
  </h2>
  <%= calendar @date do |date| %>
    <%= date.day %>
    <% if @timeslots_by_date[date] %>
      <ul>
        <% @timeslots_by_date[date].each do |timeslot| %>
          <li><%= link_to timeslot.topics.name %>) <%= link_to timeslot.starts_at %></li>          
        <% end %>
      </ul>
    <% end %>
  <% end %>
</div>
<br/>

app/controllers/timeslots_controller.rb

  def month
    @timeslots = Timeslot.all
    @timeslots_by_date = @timeslots.group_by {|i| i.starts_at.to_date}
    @date = params[:date] ? Date.parse(params[:date]) : Date.today 
  end  

路线是

Rails.application.routes.draw do
  root to: 'posts#index'

  get '/register', to: 'users#new'
  get '/login', to: 'sessions#new'
  post '/login', to: 'sessions#create'
  get '/logout', to: 'sessions#destroy'

  get '/weekly_view', to: 'timeslots#week'
  get '/monthly_view', to: 'timeslots#month'

  resources :posts
  resources :users
  resources :categories

  resources :programmes do
    resources :topics    
  end

  resources :academies do
    resources :centres do
      resources :rooms
    end
  end

  resources :intakes do
    resources :klasses
  end

  resources :timeslots

end

模型都是 has_many :通过与所有第 3 个模型中声明的 belongs_to 的关联。

class Klass < ActiveRecord::Base

  belongs_to :intake
  has_many :user_klasses
  has_many :users, through: :user_klasses
  has_many :klass_programmes
  has_many :programmes, through: :klass_programmes
  has_many :klass_timeslots
  has_many :timeslots, through: :klass_timeslots  
  has_many :klass_centres
  has_many :centres, through: :klass_centres
  has_many :klass_rooms
  has_many :rooms, through: :klass_rooms  
  has_many :klass_topics
  has_many :topics, through: :klass_topics

end

class Programme < ActiveRecord::Base

  has_many :topics
  has_many :user_programmes
  has_many :users, through: :user_programmes
  has_many :programme_timeslots
  has_many :timeslots, through: :programme_timeslots
  has_many :programme_intakes
  has_many :intakes, through: :programme_intakes  
  has_many :klass_programmes
  has_many :klasses, through: :klass_programmes   

end

class Topic < ActiveRecord::Base
  belongs_to :programme
  has_many :user_topics
  has_many :users, through: :user_topics
  has_many :topic_intakes
  has_many :intakes, through: :topic_intakes
  has_many :topic_timeslots
  has_many :timeslots, through: :topic_timeslots
  has_many :klass_topics
  has_many :klasses, through: :klass_topics

end

class Timeslot < ActiveRecord::Base
  has_many :programme_timeslots
  has_many :programmes, through: :programme_timeslots
  has_many :room_timeslots
  has_many :rooms, through: :room_timeslots
  has_many :user_timeslots
  has_many :users, through: :user_timeslots
  has_many :topic_timeslots
  has_many :topics, through: :topic_timeslots
  has_many :centre_timeslots
  has_many :centres, through: :centre_timeslots  
  has_many :klass_timeslots
  has_many :klass, through: :klass_timeslots     

end

日历助手模块如下

app/helpers/calendar_helper.rb

module CalendarHelper
  def calendar(date = Date.today, &block)
    Calendar.new(self, date, block).table
  end

  class Calendar < Struct.new(:view, :date, :callback)
    HEADER = %w[Sunday Monday Tuesday Wednesday Thursday Friday Saturday]
    START_DAY = :sunday

    delegate :content_tag, to: :view

    def table
      content_tag :table, class: "calendar" do
        header + week_rows
      end
    end

    def header
      content_tag :tr do
        HEADER.map { |day| content_tag :th, day }.join.html_safe
      end
    end

    def week_rows
      weeks.map do |week|
        content_tag :tr do
          week.map { |day| day_cell(day) }.join.html_safe
        end
      end.join.html_safe
    end

    def day_cell(day)
      content_tag :td, view.capture(day, &callback), class: day_classes(day)
    end

    def day_classes(day)
      classes = []
      classes << "today" if day == Date.today
      classes << "notmonth" if day.month != date.month
      classes.empty? ? nil : classes.join(" ")
    end

    def weeks
      first = date.beginning_of_month.beginning_of_week(START_DAY)
      last = date.end_of_month.end_of_week(START_DAY)
      (first..last).to_a.in_groups_of(7)
    end
  end
end

【问题讨论】:

  • 我在您的问题中没有看到带有回溯的完整错误消息。这种错误可能是由每个点引起的,我猜不出来。
  • 感谢您的浏览。是的,现有代码没有错误消息。也许更准确的问题是如何更改控制器中的代码,以便能够在视图中显示所需的参数。

标签: ruby-on-rails ruby ruby-on-rails-4


【解决方案1】:

topics 是一个集合,模型上的所有其他关系也是如此。您必须遍历它们才能显示它们。

如果没有正确的轨迹,更直接的问题,很难提供更多帮助。

HTH,

【讨论】:

  • 注意,感谢您的观察。如何在不引发异常的情况下提供现有代码的堆栈跟踪?是通过刷新日历页面显示服务器输出来完成的吗?
  • 是的,如果您在本地运行服务器,您可以使用 'tail -n500 log/dev.log' 从控制台中的开发日志中获取最后 500 行,或者您可以将其导出到文件:'tail -n500 log/dev > out.part.log'。或者正如您提到的查看正在运行的服务器,您可以滚动浏览输出并在那里提取 stracktrace。
  • 所以,在stackoverflow.com/questions/11122233/… 之后,我将“puts caller”放入 timeslots_controller.rb def month ... end。刷新页面,我相信它生成了堆栈跟踪,如 gist 所示。 gist.github.com/chickensmitten/9d5b6a3efed946f6b4ab
  • 设法解决了问题,感谢您的意见。虽然没有直接解决问题,但是让我学到了很多东西。
【解决方案2】:

经过一番尝试,我认为问题在于我对 Ruby 缺乏基本的了解。下面是解决问题的代码。

app/views/timeslots/month.html.erb/_month_views.html.erb

#skipped for brevity

        <% @timeslots_by_date[date].each do |timeslot| %>
          <li><%= link_to timeslot.starts_at %></li>
          <% timeslot.klass.each do |topic| %>
            <li><%= link_to topic.name %></li>
          <% end %>          
        <% end %>

#skipped for brevity

有了它,现在时间表将显示 Klass.name。其余的将被忽略,因为日历会变得笨拙。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多