0

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

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

用代码更新。

这段代码实际上是由 rails 生成的脚手架代码。希望这将有助于弄清楚我想要做什么。我只想能够将我的故事板渲染到主视图故事板 div 标签中。

家庭控制器

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>

故事板控制器

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

故事板视图

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

1 回答 1

2

#render在 Rails 中,您可以渲染任何模板:

# 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>
于 2012-12-06T16:04:22.363 回答