Note that there are some explanatory texts on larger screens.

plurals
  1. PORuby on Rails: How to get the current week and loop through it to display its days?
    primarykey
    data
    text
    <p><img src="https://i.stack.imgur.com/QQ4HB.jpg" alt="enter image description here"></p> <p>The above picture is what I mean. I think the code should be something like:</p> <pre><code> &lt;% @days_from_this_week.each do |day| %&gt; &lt;%= day.name %&gt; &lt;!--e.g. Monday--&gt; &lt;% day.sessions do |session| %&gt; &lt;% @session_categories.each do |category| %&gt; &lt;!--categories means the Sesion1/2/3/4 category A day's sessionX could be empty, so we have to determine wheather a session matches its cateogry, and then display it in the table --&gt; &lt;% if session.category == category %&gt; &lt;%= session.content %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>But how to get the <code>current week</code>? And how to get the <code>name of a day</code>? And to navigate through this calendar, <code>pre_week</code> and <code>next_week</code> is also needed.</p> <p>I found<code>prev_week</code> and <code>next_week</code> method, but it seems like return a certain day after a week, which is not I need. </p> <p>A possible solution is <a href="https://stackoverflow.com/questions/11932091/rails-get-the-current-weeks-and-months-start-and-end-dates">here</a>, but I don't know how to use it.</p> <p>UPDATE: I found a workable solution, but still looking for better code:</p> <pre><code> &lt;!-- @date = params[:date] ? Date.parse(params[:date]) : Date.today first=@date.at_beginning_of_week last=@date.at_beginning_of_week + 6 @days= (first..last).to_a--&gt; &lt;% @days.each do |day| %&gt; &lt;div&gt; &lt;%= day %&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload