Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 在partial内部将变量传递给partial_Ruby On Rails - Fatal编程技术网

Ruby on rails 在partial内部将变量传递给partial

Ruby on rails 在partial内部将变量传递给partial,ruby-on-rails,Ruby On Rails,在view.html.erb中: <%= render @posts %> <%= render partial: 'shared/block', locals: { blockedcallsign: post.user.callsign } %> <%= button_to blockrelationships_path( params: { blocker_callsign: @user.

在view.html.erb中:

<%= render @posts %>
<%= render partial: 'shared/block', locals: { blockedcallsign: post.user.callsign } %>
<%= button_to blockrelationships_path(
                params: { 
                  blocker_callsign: @user.callsign, 
                  blocked_callsign: blockedcallsign #'baz'
                },
                remote: true
              ),
              class: 'btn btn-default btn-xs' do %>
  <span class="glyphicon glyphicon-ban-circle" aria-hidden="true"></span>
<% end %>

在每个_post.html.erb中:

<%= render @posts %>
<%= render partial: 'shared/block', locals: { blockedcallsign: post.user.callsign } %>
<%= button_to blockrelationships_path(
                params: { 
                  blocker_callsign: @user.callsign, 
                  blocked_callsign: blockedcallsign #'baz'
                },
                remote: true
              ),
              class: 'btn btn-default btn-xs' do %>
  <span class="glyphicon glyphicon-ban-circle" aria-hidden="true"></span>
<% end %>

在每个_block.html.erb中:

<%= render @posts %>
<%= render partial: 'shared/block', locals: { blockedcallsign: post.user.callsign } %>
<%= button_to blockrelationships_path(
                params: { 
                  blocker_callsign: @user.callsign, 
                  blocked_callsign: blockedcallsign #'baz'
                },
                remote: true
              ),
              class: 'btn btn-default btn-xs' do %>
  <span class="glyphicon glyphicon-ban-circle" aria-hidden="true"></span>
<% end %>

在_block.html.erb中,我得到了错误:
未定义的局部变量或方法“blockedcallsign”
。当我用“baz”这样的字符串替换blockedcallsign时,它就起作用了。由于某些原因,blockedcallsign没有从_post.html.erb传递到_block.html.erb。代码有什么问题吗?

最新的方法是:

<%= render 'shared/block', { blockedcallsign: post.callsign } %>


作为散列传递

您要查找的语法是

render'shared/block',locals:{blockedcallsign:post.callsign}
in
\u posts.html.erb


然后,在
\u block.html.erb
中,您需要调用
blockedcallsign
not@blockedcallsign

代码没有问题。view.html.erb中忘记了第二个“render”,导致了该问题。对不起。

请问您的rails版本是什么?您在许多文件中使用了“blockedcallsign”标记。实际上是哪个引发了错误?是block.html.erb中的blockedcallsign导致了错误。您应该将其编辑到您的问题中。