抱歉,我是 Rails 新手
现在我正在尝试构建一个类似于https://pinboard.in的小型应用程序,我正在尝试与他们一起进行暑期实习。
这是我的书签模型
class Bookmark < ActiveRecord::Base
attr_accessible :url, :title, :description, :counter
belongs_to :user
#validates that url has https:// or http://
validates :url, :format => { :with => /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0- 9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix,
:message => "Invalid URL" }
end
这是我的书签控制器
class BookmarksController < ApplicationController
def add_bookmark
@bookmark = Bookmark.new
respond_to do |format|
format.html
end
end
def draw_recent
@bookmarks = Bookmark.all
end
end
这是我的表格
<%= form_for :bookmark do |f| %>
URL: <%= f.text_field :url %><br/>
Title: <%= f.text_field :title %><br/>
Description: <%= f.text_field :description %><br/>
<%= f.submit "Submit" %>
<% end %>
一切都正确呈现,当我输入信息并提交添加
这是我的输出
Started POST "/add" for 127.0.0.1 at 2013-05-09 09:55:58 -0400
Processing by BookmarksController#add_bookmark as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"ZCxA226pOVyu5AkQAmvvfKz5uPQ4gFggPTwrswtqZYk=", "bookmark"=> {"url"=>"http://litmus.com", "title"=>"Email marketing ", "description"=>"email marketing "}, "commit"=>"Submit"}
Rendered bookmarks/_form.html.erb (1.9ms)
Rendered bookmarks/add_bookmark.html.erb within layouts/application (3.3ms)
Completed 200 OK in 96ms (Views: 95.4ms | ActiveRecord: 0.0ms)
我脑子里有两个想法,我的表单正确发布,但不知何故它没有将任何东西保存到我的数据库中,我需要在控制器中保存方法吗?
当我尝试 @bookmark 而不是 :bookmark 时,应用程序会向我抛出一个错误,提示错误的 bookmarks.path
undefined method `bookmarks_path
我了解前者您实际上是在控制器中使用实例 @bookmark ,而后者正在环绕模型......
有人可以启发我吗?我觉得这对你们来说是微不足道的...... :)