我对我收到的一个控制器操作错误感到非常困惑。我收到的一个操作positions#one_click_create
的错误让我产生了这个错误:
ActiveRecord::StatementInvalid: PG::ObjectNotInPrerequisiteState: ERROR: cannot insert into view "contributions" DETAIL: Views that do not select from a single table or view are not automatically updatable.
HINT: To enable inserting into the view, provide an INSTEAD OF INSERT trigger or an unconditional ON INSERT DO INSTEAD rule.
: INSERT INTO "contributions" ("person_id", "project_id", "position_id", "name_fact_id") VALUES ($1, $2, $3, $4) RETURNING "id"
test/controllers/positions_controller_test.rb:420:in `block (3 levels) in <top (required)>
错误继续建议postgresql触发器INSTEAD OF INSERT触发器。
我已经阅读了INSTEAD OF INSERT中找到的相关链接,但我仍然不明白问题是什么,甚至是如何尝试解决它。我有一个简单的获取请求,我不明白它为什么不起作用。
对于初学者,我在我看来有这个代码。
- options = { project_name: contribution.project.url_name,
committer_name: contribution.contributor_fact.name.name,
invite: params[:invite] }
%button.btn.btn-primary
= link_to t('.claim'), one_click_create_account_positions_path(options.merge(account_id: 'me')), { style: 'color: white' }
代码在经过一些before_actions之前没有点击positions#one_click_create
的获取操作,以查找相关模型。
下面是one_click_create方法。
def one_click_create
result = current_user.position_core.ensure_position_or_alias!(@project, @name)
if result.class == Alias
flash[:success] = t('.one_click_commit_claim', name: @name.name, preferred_name: result.preferred_name.name)
redirect_to acount_positions_path(current_user)
elsif result.class == Position
flash[:success] = t('.one_click_contribution_success', name: @name.name)
redirect_to accounts_positions_path(current_user)
end
flash[:success] = t('.one_click_submit_form', name: @name.name)
redirect_to new_account_position_path(current_user,
project_name: @project.name,
committer_name: @name.name,
invite: params[:invite])
end
在我的特定情况下,条件逻辑并不重要。重要的是当它到达最后一行以重定向到new_account_position_path时,我收到错误,如此控制器测试所示。
it 'should successfully redirect to account new page with proper params' do
position = create_position
contribution = create(:contribution, position: position)
get :one_click_create, account_id: 'me', params: { project_name: contribution.project.url_name,
committer_name: contribution.contributor_fact.name.name,
invite: params[:invite]
must_redirect_to new_account_position_path(account_id: 'me')
end
有人可以帮助解释这里的问题是什么以及为什么它“无法插入视图:贡献”?
答案 0 :(得分:1)
视图是通过对其他真实或虚拟表执行查询而获得的虚拟表。出于这个原因,在特殊情况下可以仅修改 视图,特别是当它从单个(真实)表定义时,并且在这种情况下,在特定条件下定义。如果您的程序试图将某些内容插入到未在单个表中定义的视图中,并且满足这些特殊条件,则PostgreSQL会出错。请查看PostgreSQL手册http://www.postgresql.org/docs/9.4/interactive/sql-createview.html的“可更新视图”部分。您可以使用其他方法修改用于定义视图的(实际)表,如错误消息中的提示所述。