当前位置: 动力学知识库 > 问答 > 编程问答 >

Ruby on Rails: New post not saving on the blog

问题描述:

I'm working on an exercise, creating a blog with ruby on rails. I have the form ready to post an article, but once I click on the submit button, I am redirected to the homepage but the article doesn't save. Here is the following code

class ArticlesController < ApplicationController

def index

@articles = Article.paginate(:page => params[:page], per_page: 5).order('created_at DESC')

end

def show

@article = Article.find(params[:id])

end

def new

@article = Article.new

end

def create

@article = Article.new(title: params.permit[:title], body: params.permit[:body])

if @article.save

redirect_to articles, :notice => "Your post has been saved"

else

render :create

end

end

end

Here is the view create.html.haml

.container

.row

.col-xs-9-

.panel-title

%h2 Ecrivez votre article

= form_for @article do |f|

= f.text_field :title

= f.text_area :body, size: "60x12"

= f.submit "Publier"

Then the route.rb, I don't know if it can help

TP2::Application.routes.draw do

resources :articles, only: [:index]

get 'articles' => 'articles#index'

get 'articles/:id' => 'articles#show'

get 'articles/new'

get 'post' => 'articles#create'

post 'articles' => 'articles#index'

And to finish here is what the console show when I try to post an article

Started GET "/post" for 127.0.0.1 at 2016-04-10 14:24:56 +0200

Processing by ArticlesController#create as HTML

(0.2ms) BEGIN

(0.2ms) ROLLBACK

Rendered articles/create.html.haml within layouts/application (1.4ms)

Completed 200 OK in 9ms (Views: 4.9ms | ActiveRecord: 0.4ms)

Started POST "/articles" for 127.0.0.1 at 2016-04-10 14:25:10 +0200

Processing by ArticlesController#index as HTML

Parameters: {"utf8"=>"✓", "authenticity_token"=>"FMQmKvZ1t98ZE21VaiBQhm0jKJ9x9BwkXFbh4obfi3Qea0Zax5dgGirfpgcAiQA464GMD2+Qv/eGYrmvEoTZBQ==", "article"=>{"title"=>"Post", "body"=>"New Article test"}, "commit"=>"Publier"}

Article Load (0.6ms) SELECT "articles".* FROM "articles" ORDER BY created_at DESC LIMIT 5 OFFSET 0

(0.4ms) SELECT COUNT(*) FROM "articles"

Rendered articles/index.html.haml within layouts/application (3.4ms)

Completed 200 OK in 7ms (Views: 5.3ms | ActiveRecord: 1.0ms)

I don't understand why the new article won't save. Does anyone understand why ?

网友答案:

I would simplify the routes:

Rails.application.routes.draw do
  root to: 'articles#index' # or where you want for the first page

  resources :articles #will give you the correct path and verbs
end

And the articles_controller.rb

class ArticlesController < ApplicationController

 ...

  def create
    @article = Article.new(article_params)

    respond_to do |format|
      if @article.save
        format.html { redirect_to @article, notice: "Article created!" }
      else
        format.html { render action: 'new' }
      end
    end
  end

  private

  def article_params
    params.require(:article).permit(:title, :body)
  end
end
网友答案:

Because you are learning new stuff, this is a way you should use to debug your code:

  • put a binding.pry (a breakpoint) on the line before @article.save (or use another debugger, you can find it on Github)
  • reload your page, input the fields and click Save
  • go to the rails console, issue @article.save on the console (or @article.valid?), it should return false
  • puts @article.errors, so you can what are the validation issues

Good luck :)

分享给朋友:
您可能感兴趣的文章:
随机阅读: