我是Ruby on Rails的新手,我一直在努力开发一个简单的博客。但是,当我尝试保存新帖子时,页面会重新加载新页面,而不会保存任何数据。但是数据存在于URI中。
这是我的控制器:
class PostsController < ApplicationController
before_action :set_post, only: [:show, :edit, :update, :destroy]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
end
# GET /posts/1
# GET /posts/1.json
def show
end
# GET /posts/new
def new
@post = Post.new
end
# GET /posts/1/edit
def edit
end
# POST /posts
# POST /posts.json
def create
@post = Post.new(post_params)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /posts/1
# PATCH/PUT /posts/1.json
def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to @post, notice: 'Post was successfully updated.' }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end
# DELETE /posts/1
# DELETE /posts/1.json
def destroy
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url, notice: 'Post was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def post_params
params.require(:post).permit(:title, :category, :body)
end
end
我修改了脚手架制作的表格:
<form role="form">
<%= form_for(@post) do |f| %>
<% if @post.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@post.errors.count, "error") %> prohibited this post from being saved:</h2>
<ul>
<% @post.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label :title %><br>
<%= f.text_field :title, class: "form-control" %>
</div>
<div class="form-group">
<%= f.label :category %><br>
<%= f.select(:category, options_for_select(["Programming", "Commentary", "Book Reviews"]), {}, { class: "form-control" })%>
</div>
<div class="form-group">
<%= f.label :body %><br>
<%= f.text_area :body, class: "form-control", rows: "50" %>
</div>
<div class="actions">
<%= f.submit(class: "btn btn-primary") %>
</div>
<% end %>
</form>
这是我的模特:
class Post < ActiveRecord::Base
validates :title, presence: true
end
以下是来自服务器的日志:
Started GET "/posts/new? utf8=%E2%9C%93&authenticity_token=QfJgH82nuYVTEa1vovO4VlIjZmMeJvBLj6bkNKDrz08%3D&post%5Btitle% 5D=Hello&post%5Bcategory%5D=Programming&post%5Bbody%5D=Hello&commit=Create+Post" for 127.0.0.1 at 2014-08-11 11:38:46 -0400
Processing by PostsController#new as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"QfJgH82nuYVTEa1vovO4VlIjZmMeJvBLj6bkNKDrz08=", "post"=> {"title"=>"Hello", "category"=>"Programming", "body"=>"Hello"}, "commit"=>"Create Post"}
Rendered posts/_form.html.erb (2.3ms)
Rendered posts/new.html.erb within layouts/application (3.2ms)
Completed 200 OK in 21ms (Views: 19.1ms | ActiveRecord: 0.0ms)
答案 0 :(得分:0)
您的问题来自无效的HTML - 您有一个外部<form>
标记,然后是一个内部<form>
标记(由Rails form_for
生成)。您的浏览器遵循外部表单的指令,默认情况下只是通过GET请求提交到同一页面。
您希望浏览器遵循内部表单标记,该标记告诉浏览器向创建URL提交POST请求。因此,请删除外部form
代码。