where to put partials for rails_admin views

冷暖自知 提交于 2019-12-13 14:23:35

问题


I am trying to use a partial to customize my edit view in rails_admin. In config/initializers/rails_admin.rb I have:

RailsAdmin.config do |config|

  config.model 'Album' do
    edit do
      field :promotion do
        partial :hello_world
      end
    end
  end

end

I have also tried the other syntax that can be found on the rails_admin wiki:

RailsAdmin.config do |config|

  config.model 'Album' do
    edit do
      field :promotion do
         def render
            bindings[:view].render :partial => carrierwave.to_s, :locals => {:field => self, :form => bindings[:form]}
         end
      end
    end
  end

end

I am locating my partial at app/views/rails_admin/main/_hello_world.html.erb with the content:

<h1>Hello World!</h1>

After restarting my server nothing shows up in the edit view except for an empty 'promotion' field.

Rails_admin had a github issue for this. The discussion seemed inconclusive and the issue was closed before anyone confirmed that the solution worked for them. The issue can be viewed here:

https://github.com/sferik/rails_admin/issues/787

The moderator creates a spec for the issue and then closes it. The spec can be viewed at the very bottom of this file:

https://github.com/sferik/rails_admin/blob/59da00303ca2162089b7dd7653a2f19494fb74b4/spec/unit/config/fields/base_spec.rb

My concern with the spec is that it does not test where rails_admin looks for the partial. According to the rails_admin wiki partials should be location in app/views/rails_admin/main/

Please help me understand what I'm doing wrong or, at least, please assure me that rails_admin does not still have an issue here. I'm using Rails 4 and Ruby 2.0.0


回答1:


I'm not experienced with rspec as such but having looked at the github link for rails_admin, the article mentions 2 different ways to call a partial, and I always use the second option.

So for you I'd suggest trying to call your partial like this:

<%= render partial: "hello_world", locals: { promotion: promotion, Album: album } %>

If that doesn't work, please let me know :)




回答2:


Looking at the syntax provided in rails-admin documentation, it appears like the partial is being rendered as a string in both examples, and not as a symbol. Try changing your code to the following:

RailsAdmin.config do |config|

  config.model 'Album' do
    edit do
      field :promotion do
        partial "hello_world"
      end
    end
  end

end

If that doesn't fix the problem, I would recommend the gem jazz-hands. It's a debugging package that has pry-stack-explorer built in. I use this in tandem with binding.pry and raise in order to debug flow or to step into methods and see what's going on.

I bet you'll be able to track down what part of the gem is actually rendering the view. When using gems, I've found it pretty common to dig into the respective code base to figure out what's going on behind the scenes. Good luck!



来源:https://stackoverflow.com/questions/20651623/where-to-put-partials-for-rails-admin-views

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