【问题标题】:My views are not rendering the text that was inputted into the form我的视图没有呈现输入到表单中的文本
【发布时间】:2016-12-13 03:21:46
【问题描述】:

我正在努力学习 Rails,但由于某种原因,我的视图没有正确呈现!

当我通过网络浏览器在“文档/新”页面上的表单中输入信息时,不会呈现存储在变量中的文本。相反,它实际上是呈现的实例变量。

我正在使用 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'

非常感谢任何帮助!

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 haml


    【解决方案1】:

    我认为您的表格中的缩进不正确。试试这个:

    = 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
    

    【讨论】:

      【解决方案2】:

      在您的 show.haml 中,我认为您没有渲染变量。使用haml,您必须非常具体地进行渲染。以适当的间距将实例放在新行上

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

      如果您刚刚开始,您可以考虑切换到 erb 进行渲染,直到您了解 rails/ruby 的工作原理,然后再切换到 haml

      【讨论】:

      • 谢谢!我现在把它记下来了!
      【解决方案3】:

      您忘记在控制器的 Show 操作中定义 @doc。这就是为什么:

      @doc.title
      @doc.content
      

      按字面意思显示。

      在你的表演动作中像这样更新它:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-16
        • 1970-01-01
        • 2013-06-02
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多