Rails将变量传递给jBuilder [英] Rails passing variable to jBuilder
问题描述
已修复,解决方案已合并到OP中. @max的评论以及我相关的帖子连接到jBuilder的导轨.使用// Was:
Fixed and solutions incorporated into the OP. A combination of comments by @max here and to my related posting Rails connecting to jBuilder. Edits are with // Was:
我正在尝试通过has_many :through
关系获取值.三个主要数据库:人员,位置(具有街道地址和其他信息)以及联接表years
,联接表将人员在特定日期链接到特定地址.
I'm trying to get values through a has_many :through
relationship. Three main databases: people, locations (which has street address and other information), and the join table, years
which links the person to a particular address on a specific date.
# models/person.rb
class Person < ApplicationRecord
has_many :years, dependent: :destroy
has_many :locations, through: :years
# models/location.rb
class Location < ApplicationRecord
has_many :years
has_many :people, through: :years
# models/year.rb
class Year < ApplicationRecord
belongs_to :location
belongs_to :person
years
在特定日期将该人链接到特定地址.
years
links the person to a particular address on a specific date.
据我了解,流程是show.html.erb
调用javascript/packs/olPersonMap.js
,然后调用show_locations.json.jbuilder
.如何将person.id
传递给jbuilder?
As I understand it the flow is that the show.html.erb
calls javascript/packs/olPersonMap.js
which then calls show_locations.json.jbuilder
. How do I pass the person.id
to jbuilder?
从views/people/show.html.erb
开始(现在可能不需要类,需要重构).
From views/people/show.html.erb
(class is probably not needed now, refactoring needed).
// Was: <div id="map" class="map"></div>
<div id="map" class="map" data-url="<%= person_locations_path(@person) %>"></div>
<%= javascript_pack_tag 'olPersonMap' %>
<div>
来自javascript/packs/olPersonMap.js
.
// Near the top of the file
var urlJSON = $('#map').data('url') + '.json'
// and much further down in the code
new VectorImageLayer({
title: 'Where lived and worked',
imageRatio: 2,
source: new VectorSource({
// Was: url: '../people/show_locations',
url: urlJSON
format: new GeoJSON()
}),
来自@max
jBuilder位于app/views/people/show_locations.json.jbuilder
中,现在位于app/views/people/locations/index.json.jbuilder
中.遗漏代码不是主要问题(尽管确实有一些错误).
From @max
jBuilder was in app/views/people/show_locations.json.jbuilder
, now app/views/people/locations/index.json.jbuilder
. Leaving code out as it wasn't the main problem (although it did have few errors).
还有
# app/controllers/people/locations_controller.rb
module People
class LocationsController < ApplicationController
before_action :set_person
def index
respond_to do |f|
f.json
end
end
private
def set_person
@person = Person.eager_load(:locations)
.find(params[:person_id])
@locations = @person.locations
end
end
end
和routes.rb
resources :people do
resources :locations, only: [:index], module: :people
end
总而言之,如何将person.id传递给jBuilder?还是我会错了?
In summary, how do I pass person.id to the jBuilder? Or am I going at this all wrong?
谢谢@max.我需要努力了解控制器的功能.许多运动部件. https://secure-shore-68966.herokuapp.com/people/124
是Leaflet版本.在进行一些重构后将推送此OpenLayers版本,以删除所有试用版.
Thank you @max. I need to work on understanding what the controller is doing. Lots of moving parts. https://secure-shore-68966.herokuapp.com/people/124
is the Leaflet version. Will push this OpenLayers version after some refactoring to remove all the trial stuff.
推荐答案
答案已合并到OP中.谢谢@max.
The answer is incorporated into the OP. Thank you @max.
PS.我将olPeopleMap.js
更改为olPersonMap.js
,因为它更合逻辑.
PS. I changed olPeopleMap.js
to olPersonMap.js
as it was more logical.
这篇关于Rails将变量传递给jBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!