我正在为学生创建一个小项目,并且遇到一条错误消息,我尝试了所有可能的解决方案,但没有任何进展。
图像是各种模型,以及我如何实现各自的关联。以及Create的控制器方法和允许的参数。
我可以从服务器活动中清楚地看到已捕获了所有属性值,但是您收到一条错误消息,提示“活动必须存在” 。
如果有人帮助我,我会很高兴。
预先感谢
<%= form_with(model: user_activity, local: true) do |frm| %>
<div class="field">
<%#= frm.label :activity_id %><br />
<%= select_tag 'activity_id', options_for_select(Activity.all.collect{ |u| [u.activity_name, u.id] }) %>
</div>
<div class="field">
<%= frm.label(get_user_activity_date) %><br />
<%= frm.date_field :user_activity_date, autofocus: true%>
</div>
<div class="field">
<%= frm.label(get_user_activity_start_time) %><br />
<%= frm.time_field :user_activity_start, autofocus: true %>
</div>
<div class="field">
<%= frm.label(get_user_activity_end_time) %><br />
<%= frm.time_field :user_activity_end, autofocus: true, autocomplete: "age" %>
</div>
<div class="field">
<%= frm.label(get_user_activity_note) %><br />
<%= frm.text_area :user_activity_note, autofocus: true %>
</div>
<div class="actions">
<%= frm.submit %>
</div>
<% end %>
Started POST "/user_activities" for 127.0.0.1 at 2019-01-16 06:45:44 +0100
Processing by UserActivitiesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"qfcYksfgoAzNaO4ZQ6rsG9WFV6waUywbsaOCTePsjpV4PmXp0tofWfS01as6ArrdwyNtthCt5aLs2tNXavD/7A==", "activity_id"=>"3", "user_activity"=>{"user_activity_date"=>"2018-12-31", "user_activity_start"=>"12:02", "user_activity_end"=>"22:02", "user_activity_note"=>"Record User Activity"}
我已经尝试了您的大多数建议,但仍然没有进展,但是当我按下面的代码所示尝试以下操作时,得到了奇怪的结果,但是无法插入数据库中
def new
@user_activity = UserActivity.new
end
def create
# @user_activity = UserActivity.new(user_activity_params)
activity_id = user_activity_params[:activity_id] #fetch this value from params as it comes
puts "The activity ID which is received is -> #{activity_id.to_i}"
# puts current_user.user_id
user_activity_date = user_activity_params[:user_activity_date]
puts "Date -> #{user_activity_date}"
user_activity_start = user_activity_params[:user_activity_start]
puts "Start Time -> #{user_activity_start}"
user_activity_end = user_activity_params[:user_activity_end]
puts "End Time -> #{user_activity_end}"
user_activity_note = user_activity_params[:user_activity_note]
puts "Activity Note -> #{user_activity_note}"
@user_activity = current_user.user_activities.new(activity_id:activity_id.to_i, user_activity_date: user_activity_date.to_date,user_activity_start: user_activity_start.to_time,user_activity_end: user_activity_end.to_time, user_activity_note: user_activity_note )
end
def user_activity_params
params.require(:user_activity).permit(:activity_id, :user_activity_date, :user_activity_start, :user_activity_end, :user_activity_note)
# params.fetch(:user_activity, {}).permit(:user_id, :activity_id, user_activity: [:user_activity_date, :user_activity_start, :user_activity_end, :user_activity_note])
# params.permit(:user_id, :activity_id, :user_activity_date, :user_activity_start, :user_activity_end, :user_activity_note)
end
Started POST "/user_activities" for 127.0.0.1 at 2019-01-16 21:15:48 +0100
Processing by UserActivitiesController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"7v79xk6TW0IHNjkno+iXwzIYqqBjBMxCSodddPHUmKI/N4C9W6nkFz7qApXaQMEFJL6Qumn6BfsX/gxueMjp2w==", "user_activity"=>{"activity_id"=>"1", "user_activity_date"=>"2019-01-15", "user_activity_start"=>"20:14", "user_activity_end"=>"21:15", "user_activity_note"=>"testing to see if values can be retrieved"}, "commit"=>"Create User activity"}
Activity ID -> 1
Date -> 2019-01-15
Start Time -> 20:14
End Time -> 21:15
Activity Note -> testing to see if values can be retrieved
User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
↳ app/controllers/user_activities_controller.rb:46
(0.5ms) BEGIN
↳ app/controllers/user_activities_controller.rb:46
UserActivity Create (3.0ms) INSERT INTO `user_activities` (`created_at`, `updated_at`, `user_id`) VALUES ('2019-01-16 20:15:49', '2019-01-16 20:15:49', 1)
↳ app/controllers/user_activities_controller.rb:46
(0.5ms) ROLLBACK
↳ app/controllers/user_activities_controller.rb:46
Completed 500 Internal Server Error in 125ms (ActiveRecord: 12.4ms)
ActiveRecord::NotNullViolation (Mysql2::Error: Field 'user_activity_date' doesn't have a default value: INSERT INTO `user_activities` (`created_at`, `updated_at`, `user_id`) VALUES ('2019-01-16 20:15:49', '2019-01-16 20:15:49', 1)):
app/controllers/user_activities_controller.rb:46:in `create'
答案 0 :(得分:0)
您可以尝试将activity_id
提供给新的user_activity
@user_activity = current_user.user_activities.build(activity_id: params[:activity_id])
答案 1 :(得分:0)
#user.rb
class User < ApplicationRecord
has_many :user_activities
has_many :activities, through: :user_activities
end
#activity.rb
class Activity < ApplicationRecord
has_many :user_activities
has_many :users, through: :user_activities
end
#user_activity.rb
class UserActivity < ApplicationRecord
belongs_to :user
belongs_to :activity
end
我将您的模型复制到一个小型应用程序中,并尝试从Rails控制台进行操作。这应该使您更好地了解
Loading development environment (Rails 5.2.2)
2.4.1 :002 > Activity.last
(0.3ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
Activity Load (0.2ms) SELECT `activities`.* FROM `activities` ORDER BY `activities`.`id` DESC LIMIT 1
=> nil
2.4.1 :004 > obj = Activity.new(name: "Cricket", activity_type: "Sports")
=> #<Activity id: nil, name: "Cricket", created_at: nil, updated_at: nil, activity_type: "Sports">
2.4.1 :005 > obj.save
(0.2ms) BEGIN
Activity Create (0.4ms) INSERT INTO `activities` (`name`, `created_at`, `updated_at`, `activity_type`) VALUES ('Cricket', '2019-01-16 05:20:10', '2019-01-16 05:20:10', 'Sports')
(6.4ms) COMMIT
=> true
2.4.1 :007 > obj1 = User.new(name: "user1",city: "city1")
=> #<User id: nil, name: "user1", role_id: nil, created_at: nil, updated_at: nil, city: "city1">
2.4.1 :008 > obj1.save
(0.2ms) BEGIN
User Create (0.4ms) INSERT INTO `users` (`name`, `created_at`, `updated_at`, `city`) VALUES ('user1', '2019-01-16 05:20:59', '2019-01-16 05:20:59', 'city1')
(5.9ms) COMMIT
=> true
所以我在进行以下操作之前创建了一些背景数据,
2.4.1 :001 > user = User.last
(0.3ms) SET NAMES utf8, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
User Load (0.3ms) SELECT `users`.* FROM `users` ORDER BY `users`.`id` DESC LIMIT 1
=> #<User id: 1, name: "user1", role_id: nil, created_at: "2019-01-16 05:20:59", updated_at: "2019-01-16 05:20:59", city: "city1">
2.4.1 :002 > obj1 = user.user_activities.build
=> #<UserActivity id: nil, user_id: "1", activity_id: nil, created_at: nil, updated_at: nil>
2.4.1 :003 > obj1.save
(0.3ms) BEGIN
(0.2ms) ROLLBACK
=> false
2.4.1 :004 > obj1.errors.full_messages
=> ["Activity must exist"]
2.4.1 :005 > obj1 = user.user_activities.build(activity_id: Activity.last.id)
Activity Load (0.3ms) SELECT `activities`.* FROM `activities` ORDER BY `activities`.`id` DESC LIMIT 1
=> #<UserActivity id: nil, user_id: "1", activity_id: "1", created_at: nil, updated_at: nil>
2.4.1 :006 > obj1.save
(0.2ms) BEGIN
Activity Load (0.3ms) SELECT `activities`.* FROM `activities` WHERE `activities`.`id` = 1 LIMIT 1
UserActivity Create (0.2ms) INSERT INTO `user_activities` (`user_id`, `activity_id`, `created_at`, `updated_at`) VALUES ('1', '1', '2019-01-16 05:23:32', '2019-01-16 05:23:32')
(5.6ms) COMMIT
=> true
[“活动必须存在”],在控制台中查找此错误,原因是未将activity_id传递给user_activity对象。现在,在控制器中更新代码
activity_id = #fetch this value from params as it comes
@user_activity = current_user.user_activities.new(activity_id: activity_id)
我看到您有一个具有多个属性的user_activity_params方法,上述代码一旦起作用,请根据需要用新行进行测试。
@user_activity = current_user.user_activities.new(user_activity_params)
答案 2 :(得分:0)
根据帖子中提到的描述和发布的相关日志图像,似乎把activity_id传递给了控制器,但顺序错误,如控制器参数中声明的那样。
如下修改代码
对于HTML
<div class="field">
<%#= frm.label :activity_id %><br />
<%= select_tag 'user_activity[activity_id]', options_for_select(Activity.all.collect{ |u| [u.activity_name, u.id] }) %>
</div>
对于控制器
def create
@user_activity = current_user.user_activities.build(user_activity_params)
end
现在,activity_id将在user_activity内部传递,因此将按照activity_params方法中所述解析参数。