我是pretty的新Ruby on Rails和我一直在试图开发一个简单的博客。然而,当我尝试保存新邮,页面重新加载了新的一页,并没有数据被保存。然而该数据是present中的URI。
下面是我的控制器:
类PostsController<的ApplicationController before_action:set_post,只有:[:显示,编辑,:更新:灭] #GET /职位 #GET /posts.json 高清指数 @posts = Post.all 结束 #GET /职位/ 1 #GET /posts/1.json 高清节目 结束 #GET /职位/新 高清新 @post = Post.new 结束 #GET /职位/ 1 /编辑 高清编辑 结束 #POST /职位 #POST /posts.json DEF创建 @post = Post.new(post_params) respond_to代码做|格式| 如果@ post.save 的format.html {redirect_to时@post,通知:后已成功创建 } format.json {渲染:表演,状态:创建,地点:@post} 其他 的format.html {渲染:新} format.json {渲染JSON:@ post.errors,状态:unprocessable_entity} 结束 结束 结束 #PATCH / PUT /职位/ 1 #PATCH / PUT /posts/1.json DEF更新 respond_to代码做|格式| 如果@ post.update(post_params) 的format.html {redirect_to时@post,通知:帖子已成功更新 } format.json {渲染:表演,状态:OK,地点:@post} 其他 的format.html {渲染:编辑} format.json {渲染JSON:@ post.errors,状态:unprocessable_entity} 结束 结束 结束 #删除/职位/ 1 #删除/posts/1.json DEF破坏 @ post.destroy respond_to代码做|格式| 的format.html {redirect_to时posts_url,通知:帖子被成功摧毁 } format.json {头:} NO_CONTENT 结束 结束 私人 #使用回调分享共同的设置或动作之间的约束。 高清set_post @post = Post.find(PARAMS [:ID]) 结束 #从不信任的参数从可怕的网络,只允许白名单通过。 高清post_params params.require(:岗位).permit(:标题,:类别:车身) 结束 结束
我修改脚手架生产的形式:
<形式的作用=形与GT; <%=的form_for(@post)办| F | %> <%@如果post.errors.any? %> < DIV ID =error_explanation> < H2><%=复数(@ post.errors.count,错误)%>禁止这个帖子被保存:< / H> < UL> <%@ post.errors.full_messages.each办|留言| %> <李><%=邮件%GT;< /李> <%结束%GT; < / UL> < / DIV> <%结束%GT; < DIV CLASS =形组> <%= f.label:标题%>< BR> <%= f.text_field:标题,等级:表单控制%> < / DIV> < DIV CLASS =形组> <%= f.label:类别%GT;< BR> <%= f.select(:类,options_for_select([编程,评论,书评]){},{类:表格控})%> < / DIV> < DIV CLASS =形组> <%= f.label:身体%>< BR> <%= f.text_area:身体类:表单控制,行:50%> < / DIV> < DIV CLASS =行动> <%= f.submit(类:BTN BTN-主)%> < / DIV> <%结束%GT; < /形式GT;下面是我的模型:
类岗位<的ActiveRecord :: Base的 验证:标题,presence:真 结束下面是来自服务器的日志:
入门使用/职位/新? utf8=%E2%9C%93&authenticity_token=QfJgH82nuYVTEa1vovO4VlIjZmMeJvBLj6bkNKDrz08%3D&post%5Btitle% 5D =你好&功放;交%5Bcategory%5D =编程和放大器;后期%5Bbody%5D =你好&放大器;犯=创建+岗位为127.0.0.1,在2014年8月11日11时38分46秒-0400 处理由PostsController#新的HTML 参数:{UTF8=>中✓,authenticity_token=>中QfJgH82nuYVTEa1vovO4VlIjZmMeJvBLj6bkNKDrz08 =,后=> {标题=>你好,类别=>中编程,身体=>中你好},提交=>中创建邮报} 渲染帖子/ _form.html.erb(2.3ms) 渲染帖子/布局内new.html.erb /应用程序(的3.2ms) 完成200 OK的21ms(浏览次数:19.1ms | ActiveRecord的:0.0ms)解决方案
您的问题是从无效的HTML - 你有一个外部的<形式GT; 标记,然后内部<形式GT; 标签(由轨道产生的form_for )。您的浏览器是继外在形式,它在默认情况下是指令只是通过一个GET请求提交到同一个页面。
您希望您的浏览器遵循内部形式标记,它告诉浏览器提交一个POST请求创建的URL。所以去除外层表格标记。
I'm pretty new to Ruby on Rails and I've been trying to develop a simple blog. However when I try to save the new Post, the page reloads a new page and no data is saved. The data however is present in the URI.
Here's my controller:
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 endI modified the form produced by scaffolding:
<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>Here's my model:
class Post < ActiveRecord::Base validates :title, presence: true endHere's the logs from the server:
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)解决方案
Your problem is from your invalid HTML - you have an outer <form> tag, and then an inner <form> tag (generated by the Rails form_for). Your browser is following the directive of the outer form, which by default is just to submit to the same page via a GET request.
You want your browser to follow the inner form tag, which is telling the browser to submit a POST request to the create URL. So remove the outer form tag.
更多推荐
Ruby on Rails的:数据不节能的ActiveRecord
发布评论