Haml nesting with rendered partial in Rails - ruby-on-rails

Consider a partial.haml like this:
- haml_tag :body, {:id => #instance_var}
What I'm trying to do is to nest haml content under a rendered partial. Something like:
= render 'partial_file' do
%h1 My test
%p Trying to nest under a partial output.
But this is causing view errors like:
'nil' is not an ActiveModel-compatible object that returns a valid partial path.
Is there any similar solution to accomplish this?

I solved this per the recommendation in Rails: How to render repetitive form block? by specifying the render as :layout:
= render :layout => 'partial_file' do
%h1 My test

Related

Rendering just html with Rabl

I'm trying to figure out how to render just html with Rabl. I have an html partial without an object, so far all I can see are examples of partials with objects. If I try it without the object it just (obviously) throws an error.
The closest I got was:
node(:content) do
partial("api/v1/api/partials/tips")
end
Here is the documentation for Rabl.
UPDATE
I ended up just going with this below. Obvious right? For some reason when I tried it the first time it didn't work. So for sending only HTML in API responses, this works well.
def tips
render partial: "api/v1/api/partials/tips"
end
in a rabl view, for example show.v1.rabl you're able to render a view partial with the following (rails 4.1.2):
object #your_object
code :html do
context_scope.controller.render_to_string(
# from app/views/
partial: 'path/to/show.html.erb',
# locals (if needed)
locals: {
your_object: #your_object
}
)
end
This can be achieved by rendering an ERB template inside the Rabl node:
object #user
node(:html_content) do |user|
#user = user
template = Rails.root.to_s + '/app/views/api/users/show.html.erb'
ERB.new(File.read(template)).result(self.binding)
end

Rails 3, rendering a partial for the given controller if it exists?

In my application.html.erb layout for my app, I want to have a partial that renders if it exists for the given view. for example.
If the visitor is at http://example.com/users/show, I'd want the partial /users/_sidebar.html.erb to render.
But if the visitor were at say, http://example.com/user/locations/san_francisco, I'd want the partial /users/locations/_sidebar.html.erb to render.
So the thing here is that if there were no partial for that controller/action it would render some generic partial in my shared directory, and I'd rather not litter every single view with content_for blocks ya know?
Any ideas guys?
My solution is a bit different. Throw this in your application helper:
def render_partial_if_exists(base_name, options={})
file_name = ::Rails.root.to_s+"/app/views/layouts/_#{base_name}.html.erb"
partial_name = "layouts/#{base_name}"
else_file_name = ::Rails.root.to_s+"/app/views/layouts/_#{options[:else]}.html.erb"
else_partial_name = "layouts/#{options[:else]}"
if File.exists?(file_name)
render :partial => partial_name
elsif (options.key?(:else) and !options[:else].nil? and File.exists?(else_file_name))
render :partial => else_partial_name
end
end
Then in your view:
<%= render_partial_if_exists "page_#{controller.action_name}_sidebar", :else => "page_sidebar" %>
In an edit action, if "layouts/page_edit_sidebar" exists it renders it, otherwise it will render a standby "layouts/page_sidebar"
Sean Behan has a great post on exactly this:
http://seanbehan.com/programming/render-partial-if-file-exists/
I might move it to a helper and tweak it a bit to:
<%= render_sidebar %>
# This method could use either the rescue or the if file exists technique.
def render_sidebar
render(:partial => "/#{controller.name}/sidebar"
rescue
#default side bar
end

What statement can I use in Rails 3 to render HTML from a small static file?

I have a file in:
RAILS_ROOT/public/system/pages
It is a snippet of HTML. I would like to render it, along with other things, in one of my views.
But it seems like when I try to do a render from a view, Rails is always looking for a partial. But it doesn't pick up the file even when I name it with a leading underscore. How can I read and display this HTML snippet within a view?
have you tried with
<%= render :file => 'your/path/', :layout => false %>
inside the erb?

How to access validation messages in partials?

So, each rails project i seem to run into this problem and would be really grateful if someone could enlighten me:
With a "normal" setup when the form sits on the view immediately associated with the url/controller it is pretty straightforward, a render => :action on fail will display the validation message.
Now what i have is a form in a partial sitting on a page whose url/controller is a show/:id and not the create action of the form, the validation kicks in but i cannot get the validation message to display because i can't trigger the correct render action...
CLosest i got is a render => #object but there is no css/layout, i can pass a message through a redirect with flash[] but it feels wrong, same with jquery/client error messages...
So how can i "cleanly" display validation messages of a form in a partial (under another controller/action than the parent page)?
(thanks in advance for your help)
edit: can't paste the actual thing now but i'll do my best to explain
i have a main page e.g. article/show/01, on this page is the content of the article (#article) and then at the bottom of the page is a partial _commentform with a form to post a comment. This form is bound to a Create action of a different controller (comments controller).
Now if the form were on its own "page"/url instead of a partial, say /comment/create, i would jus do:
if #comment.save
redirect_to #comment
else
render => :create
end
and the validation would display normally.
In my case the form is in a a partial on the article/show/01 url, what should be the equivalent to the code above so that on validation fail error messages are displayed on the parent url, like "render article/show/01" ?
I am sure it is easy but i cannot get it to work (i just can redirect on success but cannot display the errors with a render)
I don't think the best way to display validations errors is to render a partial.
IMHO, the best and clean way to display errors messages using the styles/css you or your webdesigner wants is by implementing your own error_messages method in a FormBuilder.
For example, here is the error_messages method I've implemented for my latest project.
Here is an example that will output the errors list in ul/li's with some custom styles...
Just customize this and put your form builder in app/helpers...
class StandardBuilder < ActionView::Helpers::FormBuilder
def error_messages
return unless object.respond_to?(:errors) && object.errors.any?
errors_list = ""
errors_list << #template.content_tag(:span, "There are errors!", :class => "title-error")
errors_list << object.errors.full_messages.map { |message| #template.content_tag(:li, message) }.join("\n")
#template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
end
end
Then in my forms :
= form_for #post, :builder => StandardBuilder do |f|
= f.error_messages
...
No need to display/render another partial. And that's all :).
If you want to display anything (including error messages) in a partial you have two ways
1 - Define it in the controller action where the partial is called
2 - pass the message as a parameter to the partial
1 - Example
in your controller/action
if #comment.save
redirect_to #comment
else
#messages = "This is a message"
render => :create
end
in your partial
you can access the #message variable
2 - passing the variable to the partial
render :partial => “<partial name>”, :locals => { :param1 => “value”}
<partial name> – name of your partial (Ex /partials/footer)
:params1 – parameter name
“value” – value
hope this helps
thanks
sameera

Rails 3 Beta 2, Haml, Nested Layouts and LocalJumpError

Alright, I'm trying to create an app with nested templates. I'm using Rails 3 Beta 2 and Haml. I've poked around and I've decided to take the clearest approach and have structured my templates like so:
# application.html.haml
!!!
%body
%h1 Outermost Template
= yield(:foobar)
# inner.html.haml
- content_for :foobar do
%h2 Inner Template
= yield
= render :file => 'layouts/application'
# foo_controller.rb
layout 'inner'
With all of this, I get a LocalJumpError with the message no block given. The stack traces are blank and pretty unhelpful. Any ideas? Are these known issues?
give:
def inside_layout layout = 'application', &block
render :inline => capture_haml(&block), :layout => "layouts/#{layout}"
end
a try. Use like http://m.onkey.org/2009/7/7/nested-layouts
content_for blocks shouldn't contain yield. They aren't passed a block themselves, which is where your error message is coming from.

Resources