It will be first post. I'm a beginner for 4 months since I started programming. Thank you.
When creating a portfolio, you can now search for events by 8 regions using japanMap. At that time, I had a little trouble passing arbitrary parameters to the links for each region, so I posted it for confirmation by myself.
This time, I wrote the japanMap program in the application.js file.
Below is the code I wrote.
application.js
//Search page Japan map
$(function(){
//Create links in 8 regions
var areaLinks = {
1:"/user/index?sort=hokkaido",
2:"/user/index?sort=tohoku",
3:"/user/index?sort=kanto",
4:"/user/index?sort=chubu",
5:"/user/index?sort=kinki",
6:"/user/index?sort=chugoku_shikoku",
7:"/user/index?sort=kyusyu_okinawa",
};
//8 Local area designation
var areas = [
{code : 1, name: "Hokkaido", color: "#ab86c4", hoverColor: "#dfcceb", prefectures: [1]},
{code : 2, name: "Tohoku", color: "#6d93d1", hoverColor: "#91b0e3", prefectures: [2,3,4,5,6,7]},
{code : 3, name: "Kanto", color: "#f5a164", hoverColor: "#f5c09a", prefectures: [8,9,10,11,12,13,14]},
{code : 4, name: "Chubu", color: "#77e077", hoverColor: "#adedad", prefectures: [15,16,17,18,19,20,21,22,23]},
{code : 5, name: "Kinki", color: "#ffe966", hoverColor: "#fff2a3", prefectures: [24,25,26,27,28,29,30]},
{code : 6, name: "Chugoku / Shikoku", color: "#e68ccc", hoverColor: "#f0b9e0", prefectures: [31,32,33,34,35,36,37,38,39]},
{code : 7, name: "Kyushu-Okinawa", color: "#de6474", hoverColor: "#f29da9", prefectures: [40,41,42,43,44,45,46,47]},
];
//Map display settings
$("#map-container").japanMap({
width: 600,
areas : areas,
selection : "area",
borderLineWidth: 0.25,
drawsBoxLine : false,
movesIslands : true,
showsAreaName : true,
font : "MS Mincho",
fontSize : 13,
fontColor :"#777",
fontShadowColor : "white",
onSelect : function(data){
location.href = areaLinks[data.area.code];
};
});
This part above is the link.
var areaLinks = {
1:"/user/index?sort=hokkaido",
2:"/user/index?sort=tohoku",
3:"/user/index?sort=kanto",
4:"/user/index?sort=chubu",
5:"/user/index?sort=kinki",
6:"/user/index?sort=chugoku_shikoku",
7:"/user/index?sort=kyusyu_okinawa",
};
In the html.erb file, write as follows
:○○.html.erb
<%= link_to '◯◯', ○◯_path(:sort => 'hokkaido') %>
I didn't know how to write link_to in the js.erb file, so
php:○○.js.erb
1:"/user/index?sort=hokkaido"
I wrote it like this. By doing this, I was able to pass arbitrary params [: sort] parameters. The [? Sort = hokkaido] part is the parameter.
○○_controller.rb
def index
if params[:sort] == 'hokkaido'
@events = Event.where(prefecture_code: "Hokkaido")
@events = @events.page(params[:page]).per(6).order("id DESC")
The above is part of the controller. The event that distinguishes and displays the parameters is changed in the description of [if params [: sort] =='hokkaido'].
There may be some mistakes that are difficult to see in the first post, but thank you for watching until the end.
Recommended Posts