Ruby on Rails: Data not saving ActiveRecord

99封情书 提交于 2019-12-10 12:09:58

问题


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
end

I 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
end

Here'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)

回答1:


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.



来源:https://stackoverflow.com/questions/25246781/ruby-on-rails-data-not-saving-activerecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!