ruby-on-rails - 无法通过关联更新:找不到记录
问题描述
我无法使用Rails
on更新我的模型设置Postgres DB
。RecordNotFound
在AccountsController#edit
. _
我试图以不同的方式调用表格而没有成功。
更新1:表单调用修改为
<% if current_user.accounts.first.present? %>
<li class="list-group-item"><%= link_to "Account data",edit_account_path(@account, current_user) %></li>
<% end %>
更新 2:修改更改为:
<% if current_user.accounts.first.present? %>
<li class="list-group-item"><%= link_to "Account data",edit_account_path(current_user.accounts.first) %></li>
<% end %>
楷模:
class Account < ApplicationRecord
# associations
has_many :accounts_users
has_many :users, :through => :accounts_users
has_one :company
end
class AccountsUser < ApplicationRecord
# associations
belongs_to :user
belongs_to :account
end
class User < ApplicationRecord
# associations
has_many :accounts_users
has_many :accounts, :through => :accounts_users
has_one :company, :through => :accounts_users
end
帐户控制器相关操作如下:
# POST /accounts
# POST /accounts.json
def create
# create a new user if none exists
if !current_user.present?
build_resource(sign_in_params)
account = Account.find_or_create_by(org_name: params[:user] [:account])
else
# add the account for an existing user
@account = current_user.accounts.new(account_params)
end
# when a user account is create, add a company for that account
# create a default account and company
if resource.save
resource.accounts << account
if resource.current_account.companies.empty?
company = resource.current_account.companies.create({company_name: params[:user] [:account] })
else
company = resource.current_account.companies.first
end
resource.update(current_company: company.id)
end
respond_to do |format|
if current_user.save
format.html { redirect_to edit_account_url(@account), notice: 'Account was successfully created.' }
format.json { render json: @account, status: :created, location: @account }
else
format.html { render action: "new" }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
编辑和更新操作如下:
# GET /accounts/1/edit
def edit
@account = Account.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.js
format.json { render json: @account }
end
end
# PUT /accounts/1
# PUT /accounts/1.json
def update
@account = Account.find(params[:id])
respond_to do |format|
if @account.update_attributes(account_params)
format.html { redirect_to show_account_url(@account), notice: 'Your account has been updated successfully.'}
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @account.errors, status: :unprocessable_entity }
end
end
end
然后我用以下方式调用表格:
<% if current_user.accounts.first.present? %>
<li class="list-group-item"><%= link_to "Account data",edit_account_path(current_user, @account) %></li>
<% end %>
路线如下:
Rails.application.routes.draw do
authenticated :user do
root 'pages#home', as: :authenticated_root
end
root 'pages#index'
devise_for :users,
path: '',
path_names: {sign_in: 'login', sign_out: 'logout', edit: 'profile', sign_up: 'registration'},
controllers: {omniauth_callbacks: 'omniauth_callbacks', registrations: 'registrations'}
resources :accounts
...
end
我收到以下错误:
ActiveRecord::RecordNotFound in AccountsController#edit
Couldn't find Account with 'id'=2.
id
是在哪里user id
。
更新后 1. 现在的错误是:
No route matches {:action=>"edit", :controller=>"accounts", :format=>#<User id: 2, email: "test@test.com", created_at: "2018-12-11 10:51:45", updated_at: "2019-02-11 22:34:57", fullname: "Jacques", provider: nil, uid: nil, image: nil, phone_number: nil, description: "", pin: nil, phone_verified: nil, stripe_id: "", unread: 1, merchant_id: nil, activation: 2, team_id: nil>, :id=>nil, :locale=>:fr}, possible unmatched constraints: [:id]
我正在尝试为正确的用户更新用户帐户。
解决方案
推荐阅读
- python-3.x - Python3如何在抵押场景中实现货币时间价值
- python - Pandas 中按非连续月份过滤数据框的最简单方法是什么?
- excel - Excel VBA 将值添加到现有值而不是替换它?
- microservices - 微服务:如何追踪失败的服务?
- amazon-web-services - Terraform 上的 AWS:删除资源时出错:等待状态变为“已销毁”时超时
- spring - Spring Boot 中的计划方法
- java - 如何获取xml字符串资源的键和值
- php - Laravel 6.x GuzzleHttp 在控制器中流式传输,如果请求被用户中止,流不会关闭
- reactjs - 从 redux saga 获取数据的问题
- javascript - 移动菜单引导的 CSS 过渡