I am currently making my own contributions to GitHub.
I'd like to change the variable defined in Rails in JS click event, but I'm having trouble because I don't know how to do it.
Specifically, in the image above,
If you press the previous year, it will be 2018, and if you press the next year, it will be 2020.
I want to make it that month when I press the buttons 1 to 12 below it.
(I'm sorry that I haven't set up a CSS yet.)
I can do it only with JS, but I want to change the color of the day with the number of commits (in my case, depending on the number of hours I have learned from tweets on the photo calendar), like GitHub. I'm stuck because I don't know how to connect the variables between Rails and JS back and front desk.
<%today=Time.current%>
<%thisYear=today.strftime("%Y")%>
<%thisMonth=today.strftime("%m")%>
<%thisDay=today.strftime("%d")%>
<%weeks=['Sun', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Sat']%>
<%#M months ago from this month %>
<%m=0%>
<%#// Get the first day of the month %>
<%startDate=today.ago(m.month).beginning_of_month%>
<%#// Get last day of month %>
<%endDate=today.ago(m.month).end_of_month%>
<%#// Last day of month %>
<%endDayCount=endDate.strftime("%d")%>
<%#// Get the first day of the month %>
<%startDay=startDate.wday%>
<%#// Date Count %>
<%dayCount=0%>
<table>
<caption class="title">Learning calendar</caption>
<button id="prev" type="button">Previous year</button>
<caption class="year_month">%=today.ago(m.month).strftime("%Y year %m month")%>/caption>
<button id="next" type="button">Next year</button>br>
<%for in 1..12 do%>
<button><%=i%></button>
<%end%>
<tr>
<%forjin0..6 do%>
<th><%=weeks[j]%>/th>
<%end%>
</tr>
<%forwin0..5 do%>
<tr>
<%ford in 0..6 do%>
<%if(w==0&d<startDay.to_f)%>
<%#// First line before the day of the week>
<td>0</td>
<%elsif(dayCount>endDayCount.to_f-1)%>
<%#//% over the last days;
<td>0</td>
<%else%>
<td>
<p class="day">%=startDate.since(dayCount.days).strftime("%-d day")%>/p>
<p class="hours">
<%sum=0%>
<%@posts.each do | post | %>
<%if post.created_at.strftime("%Y%m%d")==startDate.since(dayCount.days).strftime("%Y%m%d")%>
<%sum+=post.hours.to_f%>
<%end%>
<%end%>
<%=sum.to_f%>
</p>>span>Time>/span>
</td>
<%dayCount+=1%>
<%end%>
<%end%>
</tr>
<%end%>
</table>
I tried using gem's gon and <%==JSON.dump()%>
, but it didn't work.
About Rails and Javascript collaboration using gon - Qiita
The programming itself is self-taught using such things as Progate, so the way the code is written may be strange.
It may also be wrong to write solidly on html.erb.
I would appreciate it if you could let me know if there is anything wrong with the code or the original way of thinking.
I apologize for the inconvenience, but I appreciate your cooperation.
Just for your information.
I thought it was most common to send data of the date with parameters.
For example
http://localhost/hoge?year=2020&month=03
If you add parameters to the URL like ↑,
js:location.search//"?year=2020&month=03"
rails:params[:year],params[:month]
You can get it in , so I think you can do the rest by reflecting the data in @posts.
It may be a little difficult to understand, so if you don't understand, please feel free to ask me.
540 Unable to install versioned in Google Colab
545 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
683 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
525 Uncaught (inpromise) Error on Electron: An object could not be cloned
546 Who developed the "avformat-59.dll" that comes with FFmpeg?
© 2024 OneMinuteCode. All rights reserved.