How to create 1 view for 2 layouts in erb? - ruby

I make a project on serve and use erb syntax.
I want to compile two files: index.html (compiled with template content) and load.html (only content). I create two layouts (_clear.html.erb (with the only yield) and _default.html.erb) and create a partial (_content_for_page.html.erb).
There are problems with compilation of index.html.erb.
According to Rails manual (3.4.3 Partial Layouts) I have to write index.html.erb:
<%= render :partial => "_content_for_page.html.erb", :layout => "_default.html.erb" %>
But it doesn't work. Only partial is compiled.
What is wrong?

First I just want to point out Serve does not utilize Rails unless the rails gem is included in the Gemfile of your project, but that will do something unexpected considering Serve organizes your project differently than Rails.
Now I don't know if this answers your question, but it may help to know partials should be rendered without the beginning underscore and are usually passed as a symbol.
<%= render :partial => :content_for_page, :layout => :default %>

Related

Using content_for and yield_content in Ramaze

Sinatra has sinatra/contrib to asist with this, what's the equivalent in Ramaze? I'm using Erubis by the way. Also, a quick Google search shows up really old pages that insist setting variables in the controllers and using them in the views.
Edit 1:
Taken from the gem documentation (http://www.sinatrarb.com/contrib/content_for.html)
You call content_for, generally from a view, to capture a block of markup giving it an identifier:
# index.erb
<% content_for :some_key do %>
<chunk of="html">...</chunk>
<% end %>
Then, you call yield_content with that identifier, generally from a layout, to render the captured block:
# layout.erb
<%= yield_content :some_key %>
I don't think Ramaze can do this natively. But you could quite easily do this manually, write a helper to do this, or even fill-in a Hash instance.
You might also want to look at partials if you need to render small chunks of HTML in loops.
You could also combine render_partial, store results in a hash, and yield it's content in the layout.
If the use case is something like rendering a sidebar, you probably want to write a helper so you take the logic out of your views.
A trivial example is here : https://github.com/Ramaze/ramaze/wiki/Adding-a-dynamic-sidebar-in-a-layout

Using ERB for Nesta layout and page templates

I'm trying to get Nesta to use .erb templates for layout and page templates, but confused as to how to go about it.
With partials, it's easy:
= haml :header, :layout => false
becomes:
= erb :header, :layout => false
However, I can't figure out how to have all my pages point to a layout.erb file instead of a layout.haml file.
According to this, .erb templates seem fully supported, though they only describe how to apply them for individual routes. While confusingly in this post it says that "features include support for Erb templates (in the views folder only, not for pages)"?
Does anyone have a better understanding of this?

Rails, Twitter Bootstrap, Simple Form, Heroku - Odd error when using ":wrapper => :prepend do" to get an add-on span

I have the following code in a form partial:
<%= f.association :client %>
<%= f.input :url, :wrapper => :prepend do %>
<%= content_tag :span, "http://www.", :class => "add-on" %>
<%= f.input_field :url, :class => 'span4' %>
<% end %>
Trying to load anything to do with this form partial e.g. new/edit etc causes Heroku to error. It works flawlessly on localhost.
The only obvious error in the Heroku logs is:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
I'm afraid I don't really understand that but thought it might help anyone trying to answer this.
If I change the form partial code it works on Heroku no problem at all e.g.
<%= f.input :url, :input_html => {:class => 'span4' } %>
Obviously I lose my nice bootstrap add-on span so I'd rather find out what the issue is rather than running around it.
Shot in the dark, but maybe an initializer that sets up the prepend wrapper (like initializers/simple_form.rb or initializers/simple_form_bootstrap.rb) is not being run by Heroku, either because it's not checked into git, or because of some environment-specific logic in the initializers or environment files.
Well after testing and some help from Dan I finally got to the bottom of it. Unfortunately, it seems to be quite localised to my situation though.
But just in case anyone stumbles across this, the error I had was the following:
ActionView::Template::Error (undefined method `source' for nil:NilClass):
That was in the log and was THE SAME when I followed Dan's advise to turn on error reporting on the production site (set consider_all_requests_local = true in config/environments/production.rb).
I thought that the error was being caused in the template, however, it was some validation rules in the model. The validation rules for me where some regex to stop the user starting the input with a certain string. They worked perfectly fine on my local, but Heroku really wasn't liking it!
I removed those validation rules and voila, my nice bootstrap add-on span was working perfectly.

Accessing the name of the page in ERB

I am building a website with Sinatra and ERB templates. Within the ERB I would like to access the name of the page being loaded and change a small part of the layout accordingly.
For example, my routes are like:
get '/' do
erb :index
end
I need something like:
<% unless page_is_index %>
<!-- HTML goes here -->
<% end %>
There's no direct relation in Sinatra between routing and controllers, so there isn't current_controller and current_action helpers like in Rails. What you can do is checking request.path.
You can specify a different layout which makes the changes you need. If nothing else, this layout could just include the main layout with more options.
get '/' do
erb :index, :layout => 'index_layout'
end

Remote Method Understanding?

In rails 3
While using remote method form submit, it affects the database twice with the same form values.
controller file:
#user_message = UserMessage.new(params[:user_message])
if #user_message.save
render :update
else
render :nothing => true
end
view file:
<%= form_tag ('/feedback/user_message'), :method =>'post', :remote=> true, :id=>'user_message' do%>
<%= hidden_field_tag 'user_message[user_id]', #user.user_id %>
<h2><%= #question %></h2>
<%= text_area_tag 'user_message[msg]',"", :size=>"40x5" %>
<%= submit_tag "Submit"%>
<% end %>
When I hit the submit button it creates two records on the table.
Why?
There may be couple of things that might be causing this
may be the ujs file is included twice may may be like
//= require jquery_ujs // expected to load from the rails-jquery gem
//= require_tree . // if any file is present in assets directory hierarchy it will be loaded
Or just a bug taking your sleep away
https://github.com/rails/jquery-ujs/issues/208
check out more solutions
Jquery Rails 3... form submits twice... deletes twice... help
Rails 3.1 remote requests submitting twice
This may also be related to the understanding of asset pipeline when and how use pecompiled assets.
I always use this config in development mode
config.server_static_assets = false
this forces the app to call the assets from app assets
and use precompiled assets from public in production mode
The problem most likely related to asset pipeline
You should precompiled the asset pipeline before.
This will create two copy of rails.js, one in your assets and one in application.js
This is a bug or gotcha in rails 3
See here http://www.ruby.code-experiments.com/blog/2011/10/another-gotcha-with-the-rails-31-asset-pipeline-or-why-are-my-jquery-ujs-ajax-requests-triggered-twi.html
Hope this help

Resources