我是 Rails 新手,我正在尝试创建一个简单的书签表:
def up
create_table :bookmarks do |t|
t.string :path, :null => false
t.integer :user_id, :null => false
t.timestamps
end
add_index :bookmarks, :user_id
add_index :bookmarks, :path
end
然后我在我的控制器中有这个:
# GET bookmark/new
# GET bookmark/new.json
def new
@bookmark = Bookmark.new
end
# POST bookmark/new
# POST bookmark/new.json
def create
@bookmark = Bookmark.new(params[:bookmark])
if @bookmark.save
flash[:success] = "Bookmark Saved"
else
flash[:failure] = "Failed !"
end
end
我的观点是:
<%= form_for :bookmark do |bookmark| %>
<%= bookmark.label :path %>
<%= bookmark.text_field :path %>
<%= bookmark.label :user_id %>
<%= bookmark.text_field :user_id %>
<%= bookmark.submit "Add bookmark" %>
<% end %>
最后运行 rake routes 得到这个路由列表:
bookmark_index GET /bookmark(.:format) bookmark#index
POST /bookmark(.:format) bookmark#create
new_bookmark GET /bookmark/new(.:format) bookmark#new
edit_bookmark GET /bookmark/:id/edit(.:format) bookmark#edit
bookmark GET /bookmark/:id(.:format) bookmark#show
PUT /bookmark/:id(.:format) bookmark#update
DELETE /bookmark/:id(.:format) bookmark#destroy
当我尝试提交表单时,我收到此错误:
Routing Error
No route matches [POST] "/bookmark/new"
Try running rake routes for more information on available routes.
编辑:
将 :bookmark 更改为 @bookmark 会引发此错误:
NoMethodError in Bookmark#new
Showing /media/wahtver/600415AD27D78282/3pces/pces/app/views/shared/_bookmark_form.html.erb where line #1 raised:
undefined method `bookmarks_path' for #<#<Class:0x00000003a48398>:0x007f1034b6b908>
Extracted source (around line #1):
1: <%= form_for @bookmark do |bookmark| %>
2: <%= bookmark.label :path %>
3: <%= bookmark.text_field :path %>
4:
问题是什么?
谢谢