我正在使用presenter对象为我的视图提供一些复杂的数据。我需要的部分数据旨在通过javascript呈现,为此我想使用gon gem将数据共享到浏览器。
这是我的(现在很简单),class SimilarityPresenter
attr_reader :similarities
def initialize(similarities)
@similarities = similarities
gon.histories = @similarities.each { |s| [s.id, s.data(10)] }
end
end
对象:
def index
period = Period.default
@similarity_presenter = SimilarityPresenter.new Similarity.all
end
当我开始行动时:
undefined local variable or method 'gon'
我得到public class Main22Activity extends AppCompatActivity {
String passedVar=null;
private TextView passedView= null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main22);
Bundle extras = getIntent().getExtras();
passedVar=getIntent ().getStringExtra(MainActivity.ID_EXTRA + "movieName");
passedVar2=getIntent ().getStringExtra(MainActivity.ID_EXTRA + "movieGroup");
passedView = (TextView) findViewById(R.id.textView3);
passedView.setText("I would like " + passedVar, passedVar2 + "everyday");
}
,
答案 0 :(得分:0)
我找到的第一种方法是将gon变量传递给演示者:
def index
period = Period.default
@similarity_presenter = SimilarityPresenter.new gon, Similarity.all
end
并在演示者中:
class SimilarityPresenter
attr_reader :similarities
def initialize(gon, similarities)
@similarities = similarities
gon.histories = @similarities.each do |s|
[s.id, s.data(10)]
end
end
end
这有效,但我必须承认,我不太喜欢在我使用演示器的每个控制器中传递变量。