【问题标题】:Rails 5 : How to insert Record in Database using Rails(HTML5)Rails 5:如何使用 Rails(HTML5) 在数据库中插入记录
【发布时间】:2017-04-02 10:59:00
【问题描述】:

注意我已经读过这个:Rails 3 : How to insert Record in Database using Rails

我正在使用 Rails 5。

请告诉我如何在数据库中插入记录。

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

如果我这样做,我会得到一个错误。是吗?

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

这行给了我一个错误,说 ruby​​ 在用户 n 之后 :action n 之后 :method 之后。我该怎么办?

请根据我的文件名 n 帮助,而不是 d 顶部链接中 ppl 的文件名。太棒了

错误

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... ... ^

【问题讨论】:

  • 请编辑您的问题并发布实际的错误消息,您对正在发生的事情的描述有点难以理解。
  • 是rails 3还是rails 5?我很困惑。
  • Rails 5 srry 纠正了它

标签: mysql ruby-on-rails ruby html ruby-on-rails-3


【解决方案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 %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 1970-01-01
    • 2011-07-16
    • 2014-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多