2016-08-03 40 views
-1

我是新来的铁轨,并想知道如何得到参数保存字符串数据库。 截至目前,唯一被保存的参数是最后一个参数,而其中一个参数是“int”类型。Rails。获取参数保存字符串分贝

以下代码,我预先感谢您的任何意见!

控制器

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> 
+0

我想你可能需要查看关于Rails的基本教程。试试:https://www.youtube.com/watch?v=OaDhY_y8WTo –

+0

同意你最好阅读一些基本指南。这些是官方的Rails指南,我强烈建议您通读它们 - 您将以这种方式快速提升您的rails知识:http://guides.rubyonrails.org/ :) –

+0

谢谢你们,我欣赏这些链接!并感谢Pradeep的答案,那就是问题所在! – Drixzy

回答

0

常用它你的错误解决它现在 纠正你的代码

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 

的错误是由于您使用不同的参数名称,然后名称形式使用。