Rails中的ActionController :: UnknownFormat错误

时间:2017-07-30 03:41:36

标签: ruby-on-rails-4

每当我尝试创建新帖子时,都会收到此错误。

  

ActionController :: Rails中的UnknownFormat错误

class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id

    respond_to do |f|
      if(@post.save)
        f.html  {redirect_to "", notice: "Post Created!"}
      else 
        # f.html {redirect_to "", notice: "Error! Post not saved"}
      end
    end
  end

  private 

  def post_params #allows certain data to be passed via post form
    params.require(:post).permit(:user_id, :create)
  end
end

Rails告诉我错误发生在第15行的respond_to块中。

这是Rails服务器所说的:

  

开始发布&#34; / posts&#34;对于24.188.104.188在2017-07-30 03:57:59 +0000无法从24.188.104.188渲染控制台!允许的网络:127.0.0.1,:: 1,127.0.0.0/127.255.255.255由PostsController处理#create as HTML参数:{&#34; utf8&#34; =&gt;&#34;✓&#34;,   &#34; authenticity_token&#34; = GT;&#34 + + 0rkRgEUi9BvIgLyRyxh7wr3VVr5F3HpgoMQVKcwrTmn9tNB6xkKNx EpxPt7l0NA4sv / lsjmwjn0ROh1gre4A ==&#34 ;,   &#34;发布&#34; =&gt; {&#34;内容&#34; =&gt;&#34; hohoh&#34;},&#34;提交&#34; =&gt;&#34;添加帖子& #34;}不允许   参数:内容用户负载(0.2ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;   用户&#34;。&#34; id&#34; =? ORDER BY&#34;用户&#34;。&#34; id&#34; ASC限制1 [[&#34; id&#34;,2]]   (0.1ms)开始事务(0.1ms)回滚事务重定向   到https://bump-jshariar.c9users.io完成302发现在21ms   (ActiveRecord:0.3ms)

     

开始GET&#34; /&#34;对于24.188.104.188在2017-07-30 03:57:59 +0000不能   从24.188.104.188渲染控制台!允许的网络:127.0.0.1,:: 1,   127.0.0.0/12/12755.255.255由PagesController处理#index为HTML在layouts / application(0.4ms)内呈现pages / index.html.erb
  用户负载(0.1ms)SELECT&#34;用户&#34;。* FROM&#34;用户&#34;用户&#34;。&#34; id&#34; =   ? ORDER BY&#34;用户&#34;。&#34; id&#34; ASC LIMIT 1 [[&#34; id&#34;,2]]已呈现   layouts / _nav_user.html.erb(0.3ms)渲染布局/ _nav.html.erb   (2.0ms)在73ms完成200 OK(浏览次数:72.5ms | ActiveRecord:0.1ms)

1 个答案:

答案 0 :(得分:-1)

它适用于:

class PostsController < ApplicationController
  def new
    @post = Post.new
  end

  def create
    @post = Post.new(post_params)
    @post.user_id = current_user.id

    respond_to do |f|
      if(@post.save)
        f.html  { redirect_to "", notice: "Post Created!" }
      else 
        # f.html { redirect_to '' }
      end
    end
  end

  private 

  def post_params #allows certain data to be passed via post form
    params.require(:post).permit(:user_id, :content)
  end
end