无法创建新帖子 [英] Can't create new post

查看:36
本文介绍了无法创建新帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击提交按钮时,它会创建带有空值的帖子.在 Rails 控制台中创建帖子时没问题.有任何想法吗?

_form.html.erb

<%= form_for(@post) do |f|%><div class="field"><%= f.label :title %><%= f.text_field :title %>

<div class="field"><%= f.label :body %><br><%= f.text_area :body %>

<div class="field"><%= f.label :user_id %><br><%= f.number_field :user_id %>

<div class="actions"><%= f.submit %>

<%结束%>

posts.controller

class PostsController <应用控制器定义索引@posts = Post.all结尾定义新@post = Post.new结尾定义创建@post = Post.new(post_params)@post.save!重定向到posts_path结尾私人的def post_paramsparams.require(:post).permit(:title, :body, :user_id)结尾结尾

解决方案

如果你不做 Post.new(post_params) 你做 Post.new(params[:post]) 对你有用吗?

检查您的服务器日志发送的内容,您可以在那里找到问题.

When I click submit button it creates post with empty values. No problem when i create post in rails console. Any ideas?

_form.html.erb

<%= form_for(@post) do |f| %>
<div class="field">
    <%= f.label :title %>
    <%= f.text_field :title %>
</div>

<div class="field">
    <%= f.label :body %><br>
    <%= f.text_area :body %>
</div>

<div class="field">
    <%= f.label :user_id %><br>
    <%= f.number_field :user_id %>
</div>

<div class="actions">
    <%= f.submit %>
</div>
<% end %>

posts.controller

class PostsController < ApplicationController

    def index
        @posts = Post.all
    end

    def new
        @post = Post.new
    end

    def create
        @post = Post.new(post_params)
        @post.save!
        redirect_to posts_path
    end

    private
    def post_params
        params.require(:post).permit(:title, :body, :user_id)
    end

end

解决方案

If instead of doing Post.new(post_params) you do Post.new(params[:post]) does that work for you ?

Check in your server log what is sent you could find the issues there.

这篇关于无法创建新帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆