Chapsterj
Chapsterj

Reputation: 6625

managing rails controllers

I have a home page HomeController this loads the home view which has some header content and other html at the top of the page. I also have a storyBoard below all that content on the page, then some other home page html content. Now the storyboard has a model view controller also. My question is how would I get my view to load into that storyboard area on the home page. Would I need to communicate from my HomeController to my StoryBoard controller to place the StoryBoard view into the home view area for where storyboard is meant to show.

I'm not sure how to go about setting up the relationships between the two.

UPDATED WITH CODE.

This code it just really the scaffolding code generated by rails. Hopefuly this will help make clear what I'm trying to do. I just want to be able to render my storyboard into the home view storyboard div tag.

HomeController

class HomeController < ApplicationController
  def index
  end

end

Home view

<body>
<div id="description">
    <div id="info">
        <p class="title">Sed ut perspiciatis unde omnis</p>
        <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo</p>
    </div>
</div>
<div id="soryboard">
  // add story board view here
</div>
<div id="section2">
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo</p>
</div>

</body>

StoryBoard Controller

class StoryboardsController < ApplicationController
  # GET /storyboards
  # GET /storyboards.json
  def index
    @storyboards = Storyboard.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @storyboards }
    end
  end

  # GET /storyboards/1
  # GET /storyboards/1.json
  def show
    @storyboard = Storyboard.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @storyboard }
    end
  end

  # GET /storyboards/new
  # GET /storyboards/new.json
  def new
    @storyboard = Storyboard.new

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @storyboard }
    end
  end

  # GET /storyboards/1/edit
  def edit
    @storyboard = Storyboard.find(params[:id])
  end

  # POST /storyboards
  # POST /storyboards.json
  def create
    @storyboard = Storyboard.new(params[:storyboard])

    respond_to do |format|
      if @storyboard.save
        format.html { redirect_to @storyboard, notice: 'Storyboard was successfully created.' }
        format.json { render json: @storyboard, status: :created, location: @storyboard }
      else
        format.html { render action: "new" }
        format.json { render json: @storyboard.errors, status: :unprocessable_entity }
      end
    end
  end

  # PUT /storyboards/1
  # PUT /storyboards/1.json
  def update
    @storyboard = Storyboard.find(params[:id])

    respond_to do |format|
      if @storyboard.update_attributes(params[:storyboard])
        format.html { redirect_to @storyboard, notice: 'Storyboard was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @storyboard.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /storyboards/1
  # DELETE /storyboards/1.json
  def destroy
    @storyboard = Storyboard.find(params[:id])
    @storyboard.destroy

    respond_to do |format|
      format.html { redirect_to storyboards_url }
      format.json { head :no_content }
    end
  end
end

StoryBoard view

<h1>Listing storyboard</h1>

<table>
  <tr>
    <th>Image url</th>
    <th>Title</th>
    <th>Description</th>
    <th>Category</th>
    <th>Link</th>
    <th>Width</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @storyboards.each do |storyboard| %>
  <tr>
    <td><%= storyboard.image_url %></td>
    <td><%= storyboard.title %></td>
    <td><%= storyboard.description %></td>
    <td><%= storyboard.category %></td>
    <td><%= storyboard.link %></td>
    <td><%= storyboard.width %></td>
    <td><%= link_to 'Show', storyboard %></td>
    <td><%= link_to 'Edit', edit_storyboard_path(storyboard) %></td>
    <td><%= link_to 'Destroy', storyboard, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<br />

<%= link_to 'New Storyboard', new_storyboard_path %>

Upvotes: 0

Views: 71

Answers (1)

krichard
krichard

Reputation: 3694

#render in Rails allows you render any template:

# home/index.html.erb
# ...
<div id="soryboard">
 <%= render :template => "storyboard/index" %>
</div>
# ...

However you must make sure that all data used in that template is also available in the HomeController action.


In addition you might want make more use of the application layout file if you havent already. Because thats usually the place where things like the header and the footer go
A typical application layout:

<!DOCTYPE html>
<html>
<head>
  <title>Title</title>
  <%= csrf_meta_tag %>
</head>
<body id="<%= params[:controller].gsub("/","_")+"_"+params[:action] %>">
  <div id="wrapper">
    <div id="header">
    </div>

    <!-- renders the corresponding template -->
    <%= yield %> 

    <div id="footer"> 
      <small>&copy; <%= Time.now.year %></small>
    </div>    
  </div>    
</body>
</html>

Upvotes: 2

Related Questions