0

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)
sevenseacat
  • 24,699
  • 6
  • 63
  • 88
DaneEH
  • 35
  • 6
  • 2
    so the error is with your model (given it happens in the console, which doesn't use the form or the controller). Can you add your model to the question? – sevenseacat Aug 11 '14 at 15:28
  • you need to implement the method `#post` as you decalred `validates :post, presence: true`. But you didn't, that's why the error. – Arup Rakshit Aug 11 '14 at 15:32
  • your Post object validates the presence of a `post` field? That doesn't make sense. – sevenseacat Aug 11 '14 at 15:33
  • Added. I barely added any changes to the model. Actually the error occurred before i placed the validations. I entered validations to see whether or not it would raise any errors but it didn't. – DaneEH Aug 11 '14 at 15:35
  • 1
    remove the `post` validation and try again. – sevenseacat Aug 11 '14 at 15:35
  • I removed the post validation and it saved in the console but my page still reloads the new page. – DaneEH Aug 11 '14 at 15:40
  • Can you post the logs from your server that show the params submitted and what happens after processing? – sevenseacat Aug 11 '14 at 15:41

1 Answers1

0

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.

sevenseacat
  • 24,699
  • 6
  • 63
  • 88