问题
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