我在Rails应用程序中遇到以下错误。你能帮我吗?
连接两个表时出现错误。 用户想要使用表格。我正在尝试从单个表中管理经理ID和用户ID字段。
红宝石2.5.1p57(2018-03-29修订版63029)[x86_64-darwin17]
Rails 5.2.2
数据库设计和表 schema.rb:
ActiveRecord::Schema.define(version: 2019_02_24_160401) do
create_table "projects", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "name"
t.text "description"
t.string "company"
t.bigint "manager_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["manager_id"], name: "index_projects_on_manager_id"
end
create_table "users", options: "ENGINE=InnoDB DEFAULT CHARSET=utf8", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "username", default: "", null: false
t.string "fullname", default: ""
t.bigint "manager"
t.string "company", default: ""
t.string "department", default: ""
t.boolean "isadmin", default: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "projects", "users", column: "manager_id"
end
projects_controller.rb
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]
before_action :find_users, only: [:index, :show, :new, :edit]
# GET /projects
# GET /projects.json
def index
@projects = Project.all
end
# GET /projects/1
# GET /projects/1.json
def show
end
# GET /projects/new
def new
@project = Project.new
end
# GET /projects/1/edit
def edit
end
# POST /projects
# POST /projects.json
def create
@project = Project.new(project_params)
respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render :show, status: :created, location: @project }
else
format.html { render :new }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
respond_to do |format|
if @project.update(project_params)
format.html { redirect_to @project, notice: 'Project was successfully updated.' }
format.json { render :show, status: :ok, location: @project }
else
format.html { render :edit }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end
# DELETE /projects/1
# DELETE /projects/1.json
def destroy
@project.destroy
respond_to do |format|
format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_project
@project = Project.find(params[:id])
end
def find_users
@users = User.all.order('created_at desc')
end
# Never trust parameters from the scary internet, only allow the white list through.
def project_params
params.require(:project).permit(:name, :description, :user_id)
end
end
模型文件夹中的模型文件 project.rb和user.rb
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many :projects
end
class Project < ApplicationRecord
belongs_to :user
end
调用我的代码电子邮件专栏 index.html.erb
<p id="notice"><%= notice %></p>
<h1>Projects</h1>
<table>
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Company</th>
<th>Manager</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @projects.each do |project| %>
<tr>
<td><%= project.name %></td>
<td><%= project.description %></td>
<td><%= project.company %></td>
<td><%= project.user.email %></td>
<td><%= link_to 'Show', project %></td>
<td><%= link_to 'Edit', edit_project_path(project) %></td>
<td><%= link_to 'Destroy', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</tbody>
</table>
<br>
<%= link_to 'New Project', new_project_path %>
答案 0 :(得分:0)
似乎数据没有在模式中草拟。 User
和Manager
是一样的东西吗?如果不是,则在user_id
表中应该有一个Projects
引用,否则,您必须添加将manager_id
绑定到Users
的适当关联,然后您才能拥有{ {1}}
答案 1 :(得分:0)
您的#include <stdio.h>
#include <ctype.h>
#include <strings.h>
#define NUM 50
#define STRINGS 100
#define OUTPUT 10
int main(void) {
char str[STRINGS][NUM+1];
int answer[NUM+1] = {0};
int carry = 0, out_digits = OUTPUT;
for(int i = 0; i < STRINGS; i++){
scanf("%s", str[i]);
for(int j = NUM; j >=0; j--){
answer[j] += (str[i][j] - 48) + carry;
if(answer[j] > 9){
carry = answer[j] / 10;
answer[j] %= 10;
}else{
carry = 0;
}
}
}
printf("--------------------------------------------------\r\n");
printf("%d",carry);
for(int j = 0; j < OUTPUT-1; j++){
printf("%d",answer[j]);
}
printf("\r\n--------------------------------------------------");
return 0;
}
有一个Project
,但没有名为manager_id
的表,而是managers
。因此,在项目与用户之间的关系中,它需要使用正确的密钥。
users
答案 2 :(得分:0)
如果用户对于Project是可选的,则只需调用project.user&.email