I am wondering how can i render in my header file a search model. Pretty much like an include in php, but with rails i want to render the new form into my header. I base my style of search on this video http://railscasts.com/episodes/111-advanced-search-form-revised but not to sure on how to render the code in to my header. Any help is appreciated
How about a partial?
Documentation
You can do something like this:
<%= render "shared/search", :locals => { :search_string => #search_string } %>
Which will render what is in the app/views/shared/_search.html.erb file.
Related
Is it possible to specify a layout for rendering JSON in Rails 3.1+ (not that I found any easy way to do it in any previous version of Rails)?
I've been using a helper like this:
def render_as_json(obj, status = 200, *args)
render(inline: obj.to_json(*args), layout: 'default', status: status)
end
It doesn't seem like render json: obj will render a layout.
I just wanted to have some metadata in the layout file:
<%- #content = yield -%>
{
"data":<%= #content.present? ? raw(#content) : '{}' %>,
"metadata":<%= raw(json_layout_metadata.to_json) %>
}
I know this is old now but I was trying to use a JSON layout and an XML layout and while searching I found your question and it helped me somehow solve my problem so here is what I did for the JSON part and the XML is pretty much the same:
Create a template file in the layout folder in your application(in the same directory of application.html.erb), call it anything but make its extension ".json.erb", let's assume you named it "json_layout.json.erb". Put in it whatever constant content you want to add and in the place you want to display dynamic data put a
<%= yield %>
In my case, this is my template:
{"status": <%= response.status %>,"data": <%= yield %>}
Now in your controller where you want to display JSON use the following format in the respond_to block:
format.json { render :inline => #the_object.to_json(any_additional_options) , :layout => "json_layout.json.erb" }
Just be careful in case of XML to put in the options of the to_xml method
:skip_instruct => true
as you would probably don't need the full XML generated by default because you add your own in the layout.
So, you can do something like that in rails:
#features.each do |feature|
render feature
end
and it will look for a partial called _feature.html.erb in the views/features folder, based on the class name.
But what if features are in a cms namespace?
Is it possible to specify the namespace? Doing something like this (it doesnt work, obviously)
render [:cms, feature]
Thx
You'll have to be more explicit:
render :partial => '/cms/feature', :object => feature
This will render the 'app/views/cms/_feature.html.erb' partial with the object being 'feature'.
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?
I have a set of partials that are used to update a section of a form depending on the user's choice from a drop-down menu. There are a lot of different choices, so rather than having a view folder like this:
app/views/myview/
_choice001.html.erb
_choice002.html.erb
...
_choice998.html.erb
_choice999.html.erb
_form.html.erb
_sharedchoicestuff1.html.erb
_sharedchoicestuff2.html.erb
edit.html.erb
new.html.erb
I want to lay it out like this:
app/views/myview/
choices/
_choice001.html.erb
_choice002.html.erb
...
_choice998.html.erb
_choice999.html.erb
_sharedchoicestuff1.html.erb
_sharedchoicestuff2.html.erb
_form.html.erb
edit.html.erb
new.html.erb
If I do that, then I know I need to change render :partial => whatever to render :partial => "myview/choices/#{whatever}" which is OK in the form, but I don't want to have to change it in all the choice templates. Is there a way to add '.' to the view path so I can still have render :partial => 'sharedchoicestuff1' in the choice templates.
Just create a helper for that:
def render_choice(name)
render "myview/choices/#{name}"
end
I want to render an HTML-EMail and send it to our customers using some ERB Templates.
The basic code I am using:
ERB.new("newsletter.html.erb").result(binding)
doesn't allow me to add partials to the html.erb-File. I would love to move the header and footer to a partial and use the render :partial-Method in that call.
Is this possible? What do I have to add?
This is what I came up with:
viewer = ActionView::Base.new(File.join(Rails::Configuration.new.view_path, "PATH/TO/PARTIALS"))
html = viewer.render(
:file => "PATH/TO/FILE.ERB),
:locals => {:variable => #var}
)
please correct me if there is a more elegant solution than this.