提交按钮根本不起作用 Simple_Forms Rails 4

Posted

技术标签:

【中文标题】提交按钮根本不起作用 Simple_Forms Rails 4【英文标题】:Submit Button not working at all Simple_Forms Rails 4 【发布时间】:2016-03-26 23:12:47 【问题描述】:

我一直试图找出为什么简单的 Posts_Controller 表单不能正常工作。我将在下面发布有问题的代码。

Posts_Controller.rb

class PostsController < ApplicationController
  before_action :find_post, only: [:show, :edit, :update, :destroy]
  before_filter :authenticate_user!, except: [:show, :index]
  before_action :correct_user, only: [:destroy]

  # GET /posts
  # GET /posts.json
  def index
    @posts = Post.order('created_at DESC')
  end

  # GET /posts/1
  # GET /posts/1.json
  def show
    @post = Post.find(params[:id])
  end

  # GET /posts/new
  def new
    @post = Post.new
  end

  # GET /posts/1/edit
  def edit
    @post = Post.find(params[:id])
    redirect_to posts_path unless @post.user == current_user
  end

  # POST /posts
  # POST /posts.json
  def create
    @post = current_user.posts.build.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

  #Only user can destroy his or her post
  def correct_user
    @post = current_user.posts.find_by(id: params[:id])
    redirect_to root_url if @post.nil?
  end

  def find_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(:content, :email, :image, :video)
  end
end

Posts_form.html.erb

<%= simple_form_for(@post, html: class: 'form-horizontal', :method => :post) do |f| %>
  <%= render 'users/shared/error_messages', object: f.object %>

  <form>
    <fieldset class="form-group">
      <label>GoPost</label>
    </fieldset>
    <fieldset class="form-group">
      <p>Add a photo</p>
      <%= f.input :image, required: false, label: false %>
    </fieldset>
    <fieldset class="form-group">
      <p>Add a video</p>
      <%= f.input :video, required: false, label: false %>
    </fieldset>
    <fieldset class="form-group">
      <%= f.input :content, required: true, autofocus: true, class:'form-control', label: false %>
    </fieldset>
  </form>
  <div class="form-group">
    <%= f.button :submit, 'Post', class: 'btn btn-primary' %>
  </div>

<% end %>

新表单呈现没有问题,但是当我单击提交按钮时无法插入数据。顺便说一句,我的视图中没有任何自定义 JS。

【问题讨论】:

【参考方案1】:

名称为 :submit 的按钮不起作用,您需要提交输入。

<%= f.submit 'Post', class: 'btn btn-primary' %>

【讨论】:

当我点击该按钮时,它没有触发。它甚至没有显示服务器日志中的更改。我只看到这个。用户负载 (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 渲染用户/shared/_error_messages.html.erb (1.0ms) 渲染帖子/_form.html.erb (50.0ms) 渲染帖子/布局/应用程序中的 new.html.erb (55.0ms) 在 492ms 内完成 200 OK (Views: 436.3ms | ActiveRecord: 2.0ms)

以上是关于提交按钮根本不起作用 Simple_Forms Rails 4的主要内容,如果未能解决你的问题,请参考以下文章

提交按钮未在 ASP.NET MVC Web 应用程序中触发

angularjs提交按钮不起作用

提交按钮在 Bootstrap 表单中不起作用

由于格式化,提交按钮不起作用

bootstrap4 CDN,提交按钮不起作用

由于提交按钮不起作用,此代码有啥问题?