Rails。获取参数以将字符串保存到数据库

Rails. Get params to save string to db

我是 rails 的新手,想知道如何获取参数以将字符串保存到数据库。 截至目前,唯一保存的参数是最后一个参数,并且是 "int" 类型。

下面的代码,我提前感谢任何评论!

控制器

def new
    #params['titel']
    mypage=Task.new 
    mypage.name=params['titel'] 
    mypage.completion=params['done']
    mypage.priority=params['priority']
    mypage.save

    redirect_to action: "tasks"

  end

数据库

class CreateTasks < ActiveRecord::Migration
  def change
    create_table :tasks do |t|

      t.timestamps null: false
    end

    add_column :tasks, :name, :string
    add_column :tasks, :completion, :string
    add_column :tasks, :priority, :int

  end
end

查看

<form action="/asterix" method="post">
    Name <input name="name">
    <br>
    Completion <input name="completion">
    <br>
    Priority <input name="priority">
    <br>
    <button type="submit"></button>
</form>

常见错误,立即解决 更正您的代码

def new
#params['titel']
mypage=Task.new 
mypage.name=params['titel'] 
mypage.completion=params['done']
mypage.priority=params['priority']
mypage.save
redirect_to action: "tasks"
end

def new
#params['titel']
mypage=Task.new 
mypage.name=params[:name] 
mypage.completion=params[:completion]
mypage.priority=params[:priority]
mypage.save
redirect_to action: "tasks"
end

错误是因为您使用的参数名称与表单中使用的名称不同。