在我的Ruby on Rails应用程序中,我有以下部分patients/_patient_link.html.erb
<a class="patient" href="<%= patient_path(patient_link.id) %>">
<span class="name"><%= patient_link.name %></span>
</a>
我正在尝试使用以下代码从控制台渲染此部分:
include Rails.application.routes.url_helpers
view = ActionView::Base.new('app/views/patients', {}, ActionController::Base.new)
output = view.render(file: '_patient_link.html', locals: {patient_link: User.last, current_user: User.last})
它返回以下错误:
ActionView::Template::Error: wrong number of arguments (given 3, expected 0..1)
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionpack-4.2.8/lib/action_dispatch/routing/url_for.rb:150:in `url_for'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionpack-4.2.8/lib/action_dispatch/routing/route_set.rb:280:in `call'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionpack-4.2.8/lib/action_dispatch/routing/route_set.rb:223:in `call'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionpack-4.2.8/lib/action_dispatch/routing/route_set.rb:345:in `block (2 levels) in define_url_helper'
from /home/mateusz/projects/dashboard/app/views/patients/_patient_link.html.erb:1:in `_app_views_patients__patient_link_html_erb__1558238916296672676_82012840'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/template.rb:145:in `block in render'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/activesupport-4.2.8/lib/active_support/notifications.rb:166:in `instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/template.rb:333:in `instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/template.rb:143:in `render'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `block in instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/activesupport-4.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/activesupport-4.2.8/lib/active_support/notifications.rb:164:in `instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:52:in `render_template'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/template_renderer.rb:14:in `render'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/renderer.rb:46:in `render_template'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/renderer/renderer.rb:27:in `render'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/actionview-4.2.8/lib/action_view/helpers/rendering_helper.rb:32:in `render'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/haml-4.0.7/lib/haml/helpers/action_view_mods.rb:12:in `render_with_haml'
from (irb):5
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/railties-4.2.8/lib/rails/commands/console.rb:110:in `start'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/railties-4.2.8/lib/rails/commands/console.rb:9:in `start'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:68:in `console'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/railties-4.2.8/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /home/mateusz/.rvm/gems/ruby-2.4.1/gems/railties-4.2.8/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
partial内部的href属性导致此错误:href="<%= patient_path(patient_link.id) %>"
。我该如何解决这个问题?
答案 0 :(得分:0)
我能够使用以下代码:
ApplicationController.new.render_to_string(
:partial => 'patients/patient_link',
:locals => { :patient_link => User.last , current_user: User.first}
)