管理rails控制器

时间:2012-12-06 16:00:13

标签: ruby ruby-on-rails-3

我有一个主页HomeController,它加载了主页视图,页面顶部有一些标题内容和其他html。我在页面上的所有内容下面都有一个storyBoard,然后是其他一些主页html内容。现在故事板还有一个模型视图控制器。我的问题是如何将我的视图加载到主页上的故事板区域。我是否需要从我的HomeController与StoryBoard控制器进行通信,以将StoryBoard视图放入故事板显示位置的主视图区域。

我不确定如何建立两者之间的关系。

更新代码。

这段代码它只是rails生成的脚手架代码。希望这有助于明确我正在努力做的事情。我只是希望能够将我的故事板渲染到主视图故事板div标签中。

的HomeController

class HomeController < ApplicationController
  def index
  end

end

主页视图

<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控制器

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视图

<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 %>

1 个答案:

答案 0 :(得分:2)

Rails中的

#render允许您渲染任何模板:

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

,您必须确保该模板中使用的所有数据也可用于 HomeController行动。


此外如果您还没有,可能需要更多地使用应用程序布局文件。因为这通常是标题和页脚之类的东西 典型的应用程序布局:

<!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>