Skip to content

Instantly share code, notes, and snippets.

@micahgodbolt
Last active December 13, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save micahgodbolt/4991146 to your computer and use it in GitHub Desktop.
Save micahgodbolt/4991146 to your computer and use it in GitHub Desktop.
Partial specific variables using get-serve
# Block syntax
<%
if !defined? title
title = "Default Title"
end
%>
<h2><%= title %></h2>
<p>Blah blah blah blah</p>
# Calling your partials
<%= render "/partials/block", :locals => { :title => "Past Events" } %>
<%= render "/partials/block", :locals => { :title => "Upcoming Events" } %>
@jedfoster
Copy link

This should do what you want.

<%= partial "/partials/block", :locals => { :title => "Past Events" } %>


<%= partial "/partials/block", :locals => { :title => "Upcoming Events" } %>

@micahgodbolt
Copy link
Author

Thanks Jed! I'm still getting "undefined local variable or method `title'" - Do I need to do something different in my partial? Does it need to be @title? Pretty new to ruby here.

@jedfoster
Copy link

No, I don't think it should be @title.

I'm assuming that

<h2><%= title %></h2>
<p>Blah blah blah blah</p>

is the content of partials/block.erb, correct?

@micahgodbolt
Copy link
Author

That's correct. I've got it rendering just fine if I remove the <%= title %> but i get an error if I put it back in.

Just to make sure you know, this is in the context of using Serve (get-serve.com) not a rails project or anything.

@jedfoster
Copy link

Just noticed I was missing the closing brace on the second example. That could cause things to blow up. Fixed.

@micahgodbolt
Copy link
Author

oh !!! hahah :) sorry! I accidently was calling it a previous time without the variable being stated.

@micahgodbolt
Copy link
Author

Yup, that does the trick! thanks.

@micahgodbolt
Copy link
Author

Oh last thing. I think it's render, not partial. Otherwise you can do <%= render :partial => "file/name" %>

@jedfoster
Copy link

Huh. I haven't used Serve, but I have used Sinatra on a lot of projects. I use sinatra-partial which gives me a partial method, which is what my example was based on.

Glad I could help. Ruby is awesome. It's a breath of fresh air after 10 years of PHP.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment