2013-03-02 52 views
2

如何從另一個頁面上的數據庫返回數據?將數據從數據庫返回到另一個導軌的頁面

我這個文件下:意見/職位/ index.htm.erb

<h1>Listing posts</h1> 

<table> 
    <tr> 
    <th>Titulo</th> 
    <th>Conteudo</th> 
    <th>Categoria</th> 
    <th>Criado em</th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% @posts.each do |post| %> 
    <tr> 
    <td><%= post.titulo %></td> 
    <td><%= post.conteudo %></td> 
    <td><%= post.category.name %></td> 
    <td><%= post.created_at.strftime("%d/%m/%Y") %></td> 
    <td><%= link_to 'Show', post %></td> 
    <td><%= link_to 'Edit', edit_post_path(post) %></td> 
    <td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td> 
    </tr> 
<% end %> 
</table> 

<br /> 

<%= link_to 'New Post', new_post_path %> 

,我想顯示另一個頁面上這些值:觀點的/ home/blog.html.erb

我該怎麼做?你能否解釋所有步驟,以便我可以在其他頁面中顯示這些值。


感謝Kocur4d

,但我怎麼只得到了一些信息?例如:我想是出現在帖子頁面像這樣的標題(位於blog.html.erb):

<div class="post-title"> 
<h2 class="title"> <a href="link_to_post"> **<% = post.titulo%>** </ a> </ h2> 
</ div> 

回答

3

步驟1.創建控制器

在您的應用程序根目錄下運行:

rails g controller home blog 

修改控制器/ homes_controller.rb

class HomesController < ApplicationController 
    def blog 
    @posts = Post.all 
    end 
end 

你的控制器/ posts_controller.rb應該已經建立。最小你需要爲你的問題是什麼來定義的索引方法,你可能有其他的方法還有:

class PostsController < ApplicationController 
    def index 
    @posts = Post.all 
    end 
end 

步驟2.提取部分

變化的意見/職位/ index.htm.erb

<h1>Listing posts</h1> 

<%= render partial: 'shared/posts', object: @posts %> 

<%= link_to 'New Post', new_post_path %> 

創建/修改的意見/家庭/ blog.html.erb

<h1>Listing posts</h1> 

<%= render partial: 'shared/posts', object: @posts %> 

<%= link_to 'New Post', new_post_path %> 

創建視圖/共享/ _posts.html.erb

<table> 
    <tr> 
    <th>Titulo</th> 
    <th>Conteudo</th> 
    <th>Categoria</th> 
    <th>Criado em</th> 
    <th></th> 
    <th></th> 
    <th></th> 
    </tr> 

<% posts.each do |post| %> 
    <tr> 
    <td><%= post.titulo %></td> 
    <td><%= post.conteudo %></td> 
    <td><%= post.category.name %></td> 
    <td><%= post.created_at.strftime("%d/%m/%Y") %></td> 
    <td><%= link_to 'Show', post %></td> 
    <td><%= link_to 'Edit', edit_post_path(post) %></td> 
    <td><%= link_to 'Delete', post, confirm: 'Are you sure?', method: :delete %></td> 
    </tr> 
<% end %> 
</table> 

步驟3.設置路由。

你應該有這樣的事情在你的routes.rb文件:

resources :posts or match 'posts/index' => 'posts#index' 

它添加到配置/路線。RB

match 'home/blog' => 'home#blog' 

所以它看起來像這樣(有幾個變種):

的config/routes.rb中

YourAppName::Application.routes.draw do 
    root to: 'posts#index' 
    resources :posts 

    match 'home/blog' => 'home#blog' 
end 

現在,當你啓動服務器導軌(假設標準配置)並訪問:

127.0.0.1:3000/posts/index127.0.0.1:3000/home/blog

你應該看到相同的內容。

這應該是複製和粘貼,但我可以犯一些錯別字和其他一些小錯誤(希望不是,如果生病了,如果生病了,儘量嘗試編輯它們)。一般來看它,因爲你需要3個步驟將http請求轉發到你的Rails應用棧。

  1. 將URL映射到使用路由的控制器。
  2. 創建控制器併爲視圖內部準備數據。
  3. 在視圖中顯示數據。

環顧四周,在Rails GuidesRails for ZombiesRails Tutorial獲取更多信息。

--------- Upadate你的第二個問題-----------

我真的不明白,你想什麼來實現?目前,index.html.erb和blog.html.erb都顯示了相同的數據,這是您要求的東西?

表示一個帖子,可在sharde/_posts.html.erb中找到。你不能從index.html.erb或blog.html.erb引用它。

@posts代表所有帖子及其在index.html.erb或blog.html.erb中可用。

渲染部分:「共享/帖」,對象:@posts - 這條線說:「嗨,夥計粘貼在這裏共享/職位文件的內容,並順便說一句我這裏有一個局部變量@posts所以如果你!需要在共享/帖子文件中使用該日期,我把它命名爲帖子內部「

爲了讓它們看起來不同,修改兩個文件和它們兩個都相同的部分在sharde/_posts.html中。 ERB。

嘗試例如刪除此行:

<td><%= post.category.name %></td> 

從共享文件,看看有什麼事情發生。

添加一些html標籤和思想家。

Rails有可能的幫助方法'找出他們檢查我給你的鏈接和谷歌,谷歌,谷歌。

嘗試添加與一些鏈接的link_to幫手

+0

你好, 我做了所有的步驟,但是當我去發佈/ index.html.erb或家庭/ blog.html.erb出現此錯誤: Home#博客中的NoMethodError 未定義的方法'each'for nil:NilClass – user1211674 2013-03-03 02:01:31

+0

我該怎麼辦? – user1211674 2013-03-03 02:02:05

+0

對不起的更改︰**呈現'共享/職位',對象:@職位** ** **呈現部分︰'共享/職位',對象:@職位**在這兩個文件中我已更新答案 – Kocur4d 2013-03-03 02:18:30

0

在您的家居控制器,對於博客的方法,集@posts因爲你需要... 也許

@posts = Post.all 
相關問題