2016-12-13 26 views
0

我正在通過學​​習Rails來擠壓我的方式,但由於某種原因,我的視圖沒有被正確渲染!我的意見不是將輸入的文本呈現爲表格

當我通過網絡瀏覽器將信息輸入到'docs/new'頁面的表單中時,文本不會被存儲在變量中。相反,它實際上是呈現的實例變量。

我使用的是simple_form gem以及haml gem。

編輯:我使用Rails 4.2.5還有C9 IDE如果有什麼差別

This is the formatting I want:

This is the formatting I'm getting:

控制器:

class DocsController < ApplicationController 

before_action :find_doc, only: [:show, :edit, :update, :destroy] 

def index 
end 

def show 
end 

def new 
    @doc = Doc.new 
end 

def create 
    @doc = Doc.new(doc_params) 

    if @doc.save 
     redirect_to @doc 
    else 
     render 'new' 
    end 
end 

def edit 
end 

def update 
end 

def destroy 
end 

private 

def find_doc 
    @doc = Doc.find(params[:id]) 
end 

def doc_params 
    params.require(:doc).permit(:title, :content) 
end 

_form.html.ha毫升:

= simple_form_for @doc do |f| 
= f.input :title 
= f.input :content 
= f.button :submit 

show.html.haml:

%h1= @doc.title 
%p= @doc.content 

new.html.haml:

%h1 New Doc! 

= render 'form' 

任何幫助是非常感謝!

回答

2

我認爲您的表單中的縮進不正確。試試這個:

= simple_form_for @doc do |f| 
    = f.input :title 
    = f.input :content 
    = f.button :submit 

And in show.html.haml: 

%h1 
    = @doc.title 
%p 
    = @doc.content 
+0

謝謝!它現在解決了! –

0

在你的show.haml中,我認爲你沒有渲染變量。隨着哈姆你必須非常具體的渲染。把實例上一個新的符合適當間距

%h1 
    = @doc.title 
%p 
    = @doc.content 

如果你是剛剛開始,你可以考慮切換到該局進行渲染,直到你得到的要點軌道/紅寶石工作,那麼如何切換到HAML後

+0

謝謝!我現在明白了! –

0

你忘了在控制器的Show動作中定義@doc。這就是爲什麼:

@doc.title 
@doc.content 

字面顯示。

在你的節目的操作更新這樣的:

def show 
    @doc = Doc.find(params[:id]) 
end 
相關問題