在Rails应用程序中创建按钮不起作用。为什么不保存到数据库?

时间:2019-10-31 21:48:59

标签: ruby-on-rails form-for belongs-to

所以这可能是重复的,但是我在修复它时遇到了很多问题。我是Rails的新手,所以它很容易成为小事。我正在制作锻炼日志,并能够在控制台中成功保存新数据,但是当我转到服务器时,它不起作用。

用户模型:

class User < ApplicationRecord
  has_secure_password
  has_many :workouts, dependent: :destroy

  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i.freeze
  validates :name, :email, presence: true
  validates :email, format: { with: VALID_EMAIL_REGEX }, uniqueness: { case_sensitive: false }
end

锻炼模型:

class Workout < ApplicationRecord
  validates :date, presence: true

  belongs_to :user
  has_many :exercises, dependent: :destroy
  accepts_nested_attributes_for :exercises, allow_destroy: true, reject_if: proc{ |attr| attr['name'].blank?}
end

锻炼控制器(某些方法已删除):

class WorkoutsController < ApplicationController
  before_action :require_login

  def new
    @workout = current_user.workouts.build(date: Date.today)
  end

  def create
    @workout = current_user.workouts.build(workout_params)
    if @workout.save
      redirect_to workouts_path, notice: 'Workout was successfully created.'
    else
      @errors = @workout.errors.full_messages
      redirect_to new_workout_path, notice: @errors
    end
  end

  private

  def workout_params
    params.fetch(:workout, {}).permit(:date, exercises_attributes: %i[_destroy id name reps weight weight_unit hours minutes seconds])
  end
end

_form(已删除练习的嵌套表格):

<div class="container-fluid">
  <%= form_with(model: @workout, local: true, authenticity_token: true) do |form| %>
    <%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
    <% if @workout.errors.any? %>
      <div id="error_explanation">
        <h2><%= pluralize(@workout.errors.count, "error") %> prohibited this workout from being saved:</h2>

        <ul>
          <% @workout.errors.full_messages.each do |message| %>
            <li><%= message %></li>
          <% end %>
        </ul>
      </div>
    <% end %>

    <div class="container-fluid align-items-center">
      <form>
        <div class="container-fluid">
          <%= form_for @workout do |f| %>
            <div class="form-group">
              <h2><%= f.label :date %></h2>
              <div style="font-size: 18pt"><%= f.date_field :date %></div>
            </div>
          <% end %>
        </div>
        <div class="actions" >
          <%= link_to "Create Workout", workouts_path, :method => :post %>
          <%= form.submit %>
        </div>
      </form>
    </div>
  <% end %>
</div>

如果您发现有任何问题或对尝试的方法有任何建议,请告诉我。我确实注意到,如果所有锻炼均以新方法完成,但不能单击按钮,则可以保存。我有两个提交按钮,因为我分别在每个按钮上进行了不同的测试。

预先感谢,如果您要我发布更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

您已将<form>...</form>标记手动添加到html中,位于此处:

<form>
  <div class="container-fluid">
    <%= form_for @workout do |f| %>
      <div class="form-group">
        <h2><%= f.label :date %></h2>
        <div style="font-size: 18pt"><%= f.date_field :date %></div>
      </div>
    <% end %>
  </div>
  <div class="actions" >
    <%= link_to "Create Workout", workouts_path, :method => :post %>
    <%= form.submit %>
  </div>
</form>

不要那样做。 form_for为您创建表单标签。