Rails - calling a partial

戏子无情 提交于 2020-12-15 06:08:44

问题


My question is so simple that I am hesitating to ask. (Why are all examples provided in the documentation unnecessary complicated?)

I want to transfer the title section for my rails project in a partial (DRY...). Very much simplified, the calling line in my show.html.erb is:

<%= render "shared/headerblock", locals: {cntrnm: @cntrlst.CountrName } %>

Let's reduce the code in the partial _headerblock.html.erb to the absolute minimum:

My Countryname is <%= cntrnm %>

Rails complains (even without the plain text containing the passed variable):

undefined local variable or method 'cntrnm' ...

What's wrong? (BTW: also just passing a string instead of the instance variable from@cntrlst produces this error.)

As far as I can see, I just copied what I found in all kinds of tutorials and blogs. - - - Obviously not ;-)


回答1:


rails have 2 cases It's up to you

Case 1:

<%= render partial "shared/headerblock", locals: {cntrnm: @cntrlst.CountrName } %>

Case 2:

<%= render "shared/headerblock", cntrnm: @cntrlst.CountrName %>


来源:https://stackoverflow.com/questions/58203466/rails-calling-a-partial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!