如何调试为什么更新不适用于 Rails 关联?
How to debug why an update won't work with Rails associations?
我正在尝试设置一个具有工作板功能的简单 rails 应用程序。我能够将作业添加到数据库中,直到我在我的作业模型和设计用户模型之间添加了关联。现在我填写表格时它不会更新数据库。
jobs_controller
class JobsController < ApplicationController
def index
@jobs = Job.all
end
def new
@job = Job.new
end
def listing
end
def listings
end
def create
@job = Job.new(params.require(:job).permit(:title, :description, :url, :user_id))
if @job.save
redirect_to root_path
else
render "new"
end
end
end
new.html.erb
<%= simple_form_for @job do |form| %>
<%= form.input :title, label: "Job title" %>
<%= form.input :description, label: "Description" %>
<%= form.input :url, label: "URL" %>
<%= form.button :submit %>
<% end %>
index.html.erb
<% @jobs.each do |job| %>
<div class="job">
<h2><%= link_to job.title, job.url %></h2>
<p><%= job.description %></p>
</div>
<% end %>
<p><%= link_to "Add a job", new_job_path %></p>
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :jobs
end
job.rb
class Job < ApplicationRecord
belongs_to :user
end
控制台中没有错误,但数据库似乎没有更新或没有更新视图。
我也运行迁移了:
class AddUserToJob < ActiveRecord::Migration[5.2]
def change
add_reference :jobs, :user, foreign_key: true
end
end
如果您不想立即将工作关联到用户,则需要将关联更改为可选,例如:
class Job < ApplicationRecord
belongs_to :user, optional: true
end
否则您需要在表单中提供 user_id 或在控制器操作中进行设置。
你也应该将这部分委托给一个单独的方法
def job_params
params.require(:job).permit(:title, :description, :url, :user_id)
end
Job.new(job_params)
您可以在 Devise 中使用 current_user
获取用户。
class JobsController < ApplicationController
# This restricts the actions to authenticated users and prevents a nil error
before_action :authenticate_user, except: [:show, :index]
# ...
def create
# this sets the user_id column
@job = current_user.jobs.new(job_params)
if @job.save
# you really should set a flash message or something to notify the user
# and possibly redirect to the show or index action instead
redirect_to root_path
else
render "new"
end
end
private
def job_params
params.require(:job)
.permit(:title, :description, :url, :user_id)
end
end
我正在尝试设置一个具有工作板功能的简单 rails 应用程序。我能够将作业添加到数据库中,直到我在我的作业模型和设计用户模型之间添加了关联。现在我填写表格时它不会更新数据库。
jobs_controller
class JobsController < ApplicationController
def index
@jobs = Job.all
end
def new
@job = Job.new
end
def listing
end
def listings
end
def create
@job = Job.new(params.require(:job).permit(:title, :description, :url, :user_id))
if @job.save
redirect_to root_path
else
render "new"
end
end
end
new.html.erb
<%= simple_form_for @job do |form| %>
<%= form.input :title, label: "Job title" %>
<%= form.input :description, label: "Description" %>
<%= form.input :url, label: "URL" %>
<%= form.button :submit %>
<% end %>
index.html.erb
<% @jobs.each do |job| %>
<div class="job">
<h2><%= link_to job.title, job.url %></h2>
<p><%= job.description %></p>
</div>
<% end %>
<p><%= link_to "Add a job", new_job_path %></p>
user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :jobs
end
job.rb
class Job < ApplicationRecord
belongs_to :user
end
控制台中没有错误,但数据库似乎没有更新或没有更新视图。
我也运行迁移了:
class AddUserToJob < ActiveRecord::Migration[5.2]
def change
add_reference :jobs, :user, foreign_key: true
end
end
如果您不想立即将工作关联到用户,则需要将关联更改为可选,例如:
class Job < ApplicationRecord
belongs_to :user, optional: true
end
否则您需要在表单中提供 user_id 或在控制器操作中进行设置。
你也应该将这部分委托给一个单独的方法
def job_params
params.require(:job).permit(:title, :description, :url, :user_id)
end
Job.new(job_params)
您可以在 Devise 中使用 current_user
获取用户。
class JobsController < ApplicationController
# This restricts the actions to authenticated users and prevents a nil error
before_action :authenticate_user, except: [:show, :index]
# ...
def create
# this sets the user_id column
@job = current_user.jobs.new(job_params)
if @job.save
# you really should set a flash message or something to notify the user
# and possibly redirect to the show or index action instead
redirect_to root_path
else
render "new"
end
end
private
def job_params
params.require(:job)
.permit(:title, :description, :url, :user_id)
end
end