Ruby on Rails,我尝试使用该应用程序创建表单,但是当我进入显示页面时。.所有内容为空

问题描述 投票:0回答:2

我是Ruby on Rails的新手,在提交表单时遇到问题。当我准备创建表单时一切正常(如第一幅图所示),但是一旦进入显示路径,我什么都没有显示输入到表单的内容(如第二幅图所示)。我哪里错了?enter image description here

enter image description here在我的todos_controller.rb中:

class TodosController < ApplicationController
def index
    @todos= Todo.all
end

def new
end

def show
    @todo = Todo.find(params[:id])
end

def create
    @todo= Todo.new(todo_params)

    @todo.save
    redirect_to @todo
end

private
    def todo_params
        params.require(:todo).permit(:task, :description)
    end
end

在我的show.html.erb中:

<p>
    <strong>Task:</strong>
    <%= @todo.task %>
    </p>

<p>
     <strong>Description:</strong>
     <%= @todo.description %>
     </p>

在我的routes.rb中:

Rails.application.routes.draw do
get 'welcome/index'

resources :todos

root 'welcome#index'
end
ruby-on-rails ruby show react-rails
2个回答
0
投票

用调试指令扩展TodosController#create,以查看实际保存到数据库的内容。

def create
    @todo= Todo.new(todo_params)

    @todo.save

    # DEBUG
    puts '>>>>>>>>>'
    puts @todo.serializable_hash
    puts '<<<<<<<<<'

    redirect_to @todo
end

0
投票

在表单页面上(todo#new),您需要具有一个对象,用于将这些属性(:task,:description)保存到数据库中。

© www.soinside.com 2019 - 2024. All rights reserved.