我的视图未呈现输入到表单中的文本
My views are not rendering the text that was inputted into the form
我正在努力学习 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.haml:
= 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'
非常感谢任何帮助!
在你的 show.haml 我认为你没有渲染变量。使用 haml,您必须非常具体地进行渲染。将实例以适当的间距换行
%h1
= @doc.title
%p
= @doc.content
如果您刚刚开始,您可以考虑切换到 erb 进行渲染,直到您了解 rails/ruby 的工作原理,然后再切换到 haml
我认为您表单中的缩进不正确。试试这个:
= 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
您忘记在控制器的显示操作中定义@doc。这就是为什么:
@doc.title
@doc.content
按字面显示。
在你的表演动作中像这样更新它:
def show
@doc = Doc.find(params[:id])
end
我正在努力学习 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.haml:
= 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'
非常感谢任何帮助!
在你的 show.haml 我认为你没有渲染变量。使用 haml,您必须非常具体地进行渲染。将实例以适当的间距换行
%h1
= @doc.title
%p
= @doc.content
如果您刚刚开始,您可以考虑切换到 erb 进行渲染,直到您了解 rails/ruby 的工作原理,然后再切换到 haml
我认为您表单中的缩进不正确。试试这个:
= 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
您忘记在控制器的显示操作中定义@doc。这就是为什么:
@doc.title
@doc.content
按字面显示。
在你的表演动作中像这样更新它:
def show
@doc = Doc.find(params[:id])
end