2015-06-18 89 views
0

好吧,這是一個noobie問題。我製作了一個表單,將數據從表單保存到數據庫中。Rails從窗體中獲取數據(保存在數據庫中)並顯示/再次使用它

我想要然後顯示在窗體中的視圖或控制器中的數據。

這是我的表單代碼。

控制器: msgs_controller.rb

class MsgsController < ApplicationController 
    def new 
    @msg = Msg.new 
    @msgs = Msg.all 
    end 

    def create 
    @msg = Msg.new(msg_params) 
    if @msg.save 
     redirect_to msgcomp_path 
    end 
    end 

def msg_params 
    params.require(:msg).permit(:name, :emails, :content, :phone) 
end 

end 

觀點: new.html.haml

.row 
    .col-md-12 
    .col-md-4 
     //Home page text 
     .text-title 
     %h1 DMS International Ltd. 

     .text-norm 
     %p London Heathrow: +44 (0)20 8897 1766 
     Suite 207, UCH House, Old Bath Road, Colnbrook, Berkshire SL3 0NW 

     %p London Gatwick: +44 (0)1293 772608 
     Unit 1, Bridge Industrial Estate, Balcombe Road, Horley, Surrey RH6 9HU 
    .col-md-8 
     %h1 Contact DMS International 

     .contactform 
     = form_for @msg do |f| 
      Full Name: 
      %br 
      = f.text_field :name 
      %br 
      Email: 
      %br 
      = f.text_field :emails 
      %br 
      Phone Number: 
      %br 
      = f.text_field :phone 
      %br 
      How can we help you ? 
      %br 
      = f.text_field :content, :class => 'content' 
      %br 
      = f.submit 'submit', :class => 'btn btn-primary' 

    TEMP (will be replaced with email) Msgs: 
    - @msgs.each do |msg| 
     %ul 
     = msg.name 
     %br 
     = msg.emails 
     %br 
     = msg.content 
     %br 
     = msg.phone 
     %br 

現在我想要做的是從數據庫中的數據不僅爲這一觀點但通過做類似的東西說網頁? (@ msg.emails)但我不知道該怎麼做。

我希望能夠得到它,所以我可以通過電子郵件從窗體收集的人員信息。

現在,我真的很感謝你在這件事上的幫助。如果我自己找到解決方案,我也會保持更新,但我一直在尋找小時/一天以上。謝謝。

回答

0

在Rails中,ID列在顯示資源時按慣例使用。 因此,可以說,你有以下途徑認定中:

resources :msgs, only: [:show, :index, :new, :create]

這將創建下列路線:

POST /msgs  | msgs#create 
GET /msgs/new | msgs#new 
GET /msgs/:id | msgs#show 
GET /msgs  | msgs#index 

公告顯示路線的:id部分 - 它什麼叫做動態段。

現在在我們的控制器,我們可以創建缺少的動作:

class MsgsController < ApplicationController 

    def show 
    @msg = Msg.find(params[:id]) 
    end 

    def index 
    @msg = Msg.all 
    end 

    def new 
    @msg = Msg.new 
    @msgs = Msg.all 
    end 

    def create 
    @msg = Msg.new(msg_params) 
    if @msg.save 
     # Redirects to our new show action 
     redirect_to @msg 
    end 
    end 

    def msg_params 
    params.require(:msg).permit(:name, :emails, :content, :phone) 
    end 
end 

在另一個控制器的方法查詢的工作方式完全相同。 查找郵件到我們可以做這樣的事情一個特定的電子郵件地址:

@msgs = Msg.where(email: '[email protected]') 

編輯:

有在不同的控制器查詢沒有區別。

如果我想在我的根行動Pages#home來顯示所有信息,例如:

class PagesController < ApplicationController 
    def home 
    @msgs = Msg.all 
    end 
end 

-# app/views/pages/home.html.haml 
- @msgs.each do |msg| 
    p= msg.emails 

瞭解更多:

+0

您應該首先閱讀Rails指南文章,至少簡要介紹一下,或者參閱初級教程。 – max

+0

嗨,我相信我。問題是,我覺得我想失去一些東西。基本上,您可以在視圖中看到new.html.erb,例如,我可以獲得 = msg.emails 。我希望能夠以不同的觀點得到這個。例如在主頁上顯示此信息。難道不可能做出這樣的工作嗎? – Morphasis

+0

編輯答案,在不同的控制器中查詢沒有區別,我真的不明白爲什麼你對此感到困惑。也許你應該做一些教程,以便你理解參數之間的聯繫,查詢和傳遞數據到視圖。 – max

相關問題