我一直在尝试让这个表单工作多年,我似乎无法让它工作。最令人讨厌的部分是一切似乎都正常工作,但数据库中没有任何内容。
控制器:
def create
@post = Post.new(params[:post])
respond_to do |format|
if @post.save!
format.html { redirect_to @post, notice: 'Lesson was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
模型
attr_accessible :userID, :groupID, :postTime, :postText
查看添加帖子
#uploadStyle
= form_for Post.new do |f|
= f.label :postText, 'PostText'
= f.text_area :postText
%br
= f.label :postTime, 'PostTime'
= f.text_area :postTime
%br
= f.label :userID, 'UserID'
= f.text_area :userID
%br
= f.label :groupID, 'GroupID'
= f.text_area :groupID
%br
= submit_tag 'Submit'
日志
Started POST "/posts" for 127.0.0.1 at 2013-10-10 22:04:32 -0700
Processing by PostsController#index as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"AUq6mt54N7kA67M1L9wQdqhl2UPRxhOEZOv/LSowcJU=", "post"=>{"postText"=>"work please", "postTime"=>"2013-01-01 04:24:23", "userID"=>"1", "groupID"=>"1"}, "commit"=>"Submit"}
Post Load (0.1ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
Post Load (0.2ms) SELECT "posts".* FROM "posts"
Rendered posts/index.html.haml within layouts/application (12.5ms)
Completed 200 OK in 40ms (Views: 24.8ms | ActiveRecord: 0.7ms)
路线
root :to => 'posts#index' match '/posts', :to => 'posts#index' resources :posts do member do post 'upload' end end match '/posts/:id', :to => 'posts#show', :format => false match '/add_post', :to => 'posts#add_post'
答案 0 :(得分:1)
问题出在你的路线上 - 那里有很多相互矛盾的事情。
资源路由是在Rails中进行路由的首选方式 - 即resources :posts
部分。如果您只使用它,则会获得索引,新建,创建,显示,编辑,更新和删除的路由 - 其中一些与您对match /posts
,match /posts/:id
和{{1]的路径重叠}}
如果您删除这些代码,您的代码应该按预期工作,并且提交表单将由您的控制器的match /add_post
操作处理。
有关路由的更多信息:http://guides.rubyonrails.org/routing.html
编辑:您还可以通过在命令行中运行create
来查看系统中的所有路由。在输出中,您会看到rake routes
与PostsController的索引操作相匹配,这是您所看到的,它应该与POST /posts
操作匹配。