Multiple Level Nested Layout in Rails 3

Multiple Level Nested Layout in Rails 3

I reread the link i posted ( http://guides.rubyonrails.org/layouts_and_rendering.html#using-nested-layouts ) and realized I missed a key detail.

<%= render :file => 'layouts/application' %>

so, in Site::BaseController I have a call to layout 'site' and in /layouts/site.html.haml I have

= content_for :footer do
-#Content for footer
= render :file => 'layouts/application'

Then in Site::BlogController which extends Site::BaseController I have layout 'site/blog' and in /layouts/site/blog.html.haml I have

=content_for :header do
%h1 HELLO WORLD!

= render :file => 'layouts/site'

This then renders the layouts nested as described in the question. Sorry for missing this in my question. I should've read closer.

Rails 3, nested multi-level forms and has_many through

To be honest, i have never tried to edit or create a has_many :through in that way.

It took a little while, and had to fix the js inside formtastic_cocoon to get it working, so here is a working solution.

You need to specift the EventUser model, and then fill the User model (the other way round will never work).

So inside the models you write:

class Event < ActiveRecord::Base
has_many :event_users
has_many :users, :through => :event_users
accepts_nested_attributes_for :users, :reject_if => proc {|attributes| attributes[:name].blank? }, :allow_destroy => true
accepts_nested_attributes_for :event_users, :reject_if => proc {|attributes| attributes[:user_attributes][:name].blank? }, :allow_destroy => true
end

class EventUser < ActiveRecord::Base
belongs_to :user
belongs_to :event
accepts_nested_attributes_for :user
end

class User < ActiveRecord::Base
has_many :events, :through => :event_users
has_many :event_users
end

Then the views. Start with the events/_form.html.haml

= semantic_form_for @event do |f|
- f.inputs do
= f.input :name

%h3 Users (with user-type)
#users_with_usertype
= f.semantic_fields_for :event_users do |event_user|
= render 'event_user_fields', :f => event_user
.links
= link_to_add_association 'add user with usertype', f, :event_users

.actions
= f.submit 'Save'

(i ignore errors for now)
Then, you will need to specify the partial _event_user_fields.html.haml partial (here comes a little bit of magic) :

.nested-fields
= f.inputs do
= f.input :user_type, :as => :hidden, :value => 'participating'
- if f.object.new_record?
- f.object.build_user
= f.fields_for(:user, f.object.user, :child_index => "new_user") do |builder|
= render("user_fields", :f => builder, :dynamic => true)

and to end the _user_fields partial (which does not really have to be a partial)

.nested-fields
= f.inputs do
= f.input :name

This should work.
Do note that i had to update the formtastic_cocoon gem, so you will need to update to version 0.0.2.

Now it would be easily possible to select the user_type from a simple dropdown, instead of a hidden field, e.g. use

= f.input :user_type, :as => :select, :collection => ["Participator", "Organizer", "Sponsor"]

Some thoughts (now i proved it works):

  • this will always create new users on the fly, actually eliminating the need for the EventUser. Will you allow selecting existing users from a dropdown too?
  • personally i would turn it around: let users assign themselves to an event!

Nested Layout for Nested Resources in Rails

Editing the original question for clarity (the answers were not quite answering the central problem), I realized what I need to do is have the nested controller classes inherit from the top-level parent. Not only does this make solving the central issue easier, it fixes a few other things that have been nagging me.

(I'd say "a'doy" but there are others working on this app, which obscured what would normally be a bit more obvious.)

Rails 3 Nested Forms - Multi-Level

Change this:

 <%=f.fields_for :address do |address_form| %>

To this:

<%=profile_form.fields_for :address do |address_form| %>

Nested layouts in ruby on rails

I've worked out the solution, although it's not what's given in this article

I've replaced this line

<% render :file => 'layouts/application' %>

with

<%= render :file => 'layouts/application' %>

I'm not sure if the article is wrong, or I've found the wrong way to fix it! Please let me know!

Cheers

Nested layout inside application layout

Update: Based on the comments I've updated the answer with a better understanding of the question

The best way is to incorporate this into your application.html.erb layout.

The desired behavior is to have the admin menu appear when the user clicks on the Admin Panel link or any links on the admin menu.

The way I recommend doing this is that you have an admin controller which handles routing to all of your admin views, so clicking on the Admin Panel button and all the links in the admin menu will be handled by your admin controller. Add a before_filter to you admin controller like this:

# app/controller/admin_controller.rb
class AdminController < ActionController::Base
before_filter: set_admin_status

private
def set_admin_status
@admin = true
end
end

In your application template do the following:

# application.html.erb
<body>
<%= render 'layouts/header' %>
<div class="container">
<% if @admin %>
<%= render 'admins/menu' %>
<% end %>
<%= yield %>
<%= render 'layouts/footer' %>
</div>
</body>

What this should do is that everytime you navigate to the page that corresponds to the Admin Panel or any of the links in your admin menu it will set the @admin_status flag to be true and your layout will render the admin menu, which I believe is the desired behavior.



Related Topics



Leave a reply



Submit