rails 应用程序中的创建按钮无法正常工作。为什么不保存到数据库?
Create Button in rails app isn't working. Why won't it save to the db?
所以这可能是重复的,但我在修复它时遇到了很多问题。我是 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>
如果您发现任何问题或对尝试的内容有任何建议,请告诉我。我确实注意到,如果全部以新方法完成而不是通过单击按钮,我可以保存锻炼。我有两个提交按钮,因为我在每个按钮上测试不同的东西。
提前致谢,如果您需要我post更多信息,请告诉我。
您已将 <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
为您创建表单标签。
所以这可能是重复的,但我在修复它时遇到了很多问题。我是 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>
如果您发现任何问题或对尝试的内容有任何建议,请告诉我。我确实注意到,如果全部以新方法完成而不是通过单击按钮,我可以保存锻炼。我有两个提交按钮,因为我在每个按钮上测试不同的东西。
提前致谢,如果您需要我post更多信息,请告诉我。
您已将 <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
为您创建表单标签。