2016-11-18 26 views
-1

注:我已經看過此數據庫中插入記錄:Rails 3 : How to insert Record in Database using RailsRails的5:如何使用Rails(HTML5)

我使用Rails 5。

請讓我知道如何在數據庫中插入記錄。

我uisng MYSQL,以下是我的表結構,在DB文件夾正確schema.rb

ActiveRecord::Schema.define(version: 20161107113839) do 

    `create_table "allusers", force: :cascade, options: "ENGINE=InnoDB `DEFAULT CHARSET=latin1" do |t|` 
    t.string "username", limit: 50, null: false 
    t.string "email",  limit: 50 
    t.string "password", limit: 50 
    t.string "likedvideos", limit: 50 
    t.datetime "created_at",    null: false 
    t.datetime "updated_at",    null: false 
    end 

end 

這是我的控制器文件user1_controller.rb

class User1Controller < ApplicationController 
def user1index 
end 

這是我的模型文件alluser.rb

class Alluser < ApplicationRecord 
end 

這是我在\ app \ views \ user1 \ user1index.html下的視圖文件.erb

<form <% @user1,:action => :new, :method => :post %> > 
    username: <input type="text" name="username" /><br /> 
    email: <input type="text" name="email" /><br/> 
    password:<input type="password" name="password" /><br/> 
    likedvideos: <input type="text" name="likedvideos" /><br/> 

    </form> 

如果我這樣做,我得到一個錯誤。 Y?

<form <% @user1,:action => :new, :method => :post %> > 

這行給了我一個錯誤,說ruby在用戶n後預期:action n after:方法。我該怎麼辦 ?

根據我的文件名plz幫助,而不是d頂部鏈接中的ppl文件名。日Thnx

錯誤

home/vaibhav/MusicDirectory/app/views/user1/user1index.html.erb:46: syntax error, unexpected =>, expecting &. or :: or '[' or '.' ...orm '.freeze; @user1 ,:action=> :new ,:method => :post ;@out... ...^
+0

請編輯您的問題,並張貼實際的錯誤信息,您所發生的事情的描述是有點硬了解。 – guiniveretoo

+0

它是軌道3還是軌道5?我很困惑。 –

+0

Rails 5 srry糾正它 –

回答

1

你的形式應該是這個樣子:

<%= form_for @user1 do |u| %> 
<%= u.label :username %>: 
<%= u.text_field :username %><br /> 
<%= u.label :email %>: 
<%= u.text_field :email %><br /> 
<%= u.label :password %>: 
<%= u.password_field :password %><br /> 
<%= u.label :liked_videos %>: 
<%= u.text_field :liked_videos%><br /> 
<%= u.submit %> 
<% end %>