gpt4 book ai didi

ruby-on-rails - 嵌套对象 has_one 关系

转载 作者:数据小太阳 更新时间:2023-10-29 07:59:04 25 4
gpt4 key购买 nike

我是 Rails 的新手,我想用 2 个对象和 has_one 关系做一个 crud,我有一个 Project 和 Album ,每个对象只有一个 :name 属性。但是当我创建一个项目时,两个名称都是空白的。这是我的代码:

项目.rb , 专辑.rb

class Project < ActiveRecord::Base
has_one :album
accepts_nested_attributes_for :album, allow_destroy: true
end

class Album < ActiveRecord::Base
belongs_to :project
end

项目 Controller .rb

def new
@project = Project.new
@album = @project.build_album
end

def create
@project = Project.new
@album = @project.create_album(params[:album])

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

def project_params
params.require(:project).permit(:name, album_attributes: [:name])
end

_form.html.erb(项目)

<%= form_for(@project) do |f| %>
<% if @project.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@project.errors.count, "error") %> prohibited this project from being saved:</h2>
<ul>
<% @project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= f.label :name, 'Project name: ' %><br>
<%= f.text_field :name %>
</div>

<%= f.fields_for :album do |a| %>
<div class="field">
<%= a.label :name, 'Album name' %><br />
<%= a.text_field :name %>
</div>
<% end %>

<div class="actions">
<%= f.submit %>
</div>

路线.rb

  resources :projects do
resources :albums
end

由表格提交

    Started POST "/projects" for ::1 at 2016-01-22 18:39:16 -0200
Processing by ProjectsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"asJbsifN/NB2/LGY0xb8S0+OnsRlJMJqhVe3vUOzEyLPAMQN1VFcDiFOwSNcXu+V1wZ78obnc6uaacCzxDdW8A==", "project"=>{"name"=>"test", "album_attributes"=>{"name"=>"test"}}, "commit"=>"Create Project"}
(0.2ms) begin transaction
SQL (0.3ms) INSERT INTO "projects" ("name", "created_at", "updated_at") VALUES (?, ?, ?) [["name", "test"], ["created_at", "2016-01-22 20:39:16.515028"], ["updated_at", "2016-01-22 20:39:16.515028"]]
SQL (0.1ms) INSERT INTO "albums" ("name", "project_id", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "test"], ["project_id", 7], ["created_at", "2016-01-22 20:39:16.517545"], ["updated_at", "2016-01-22 20:39:16.517545"]]
(0.6ms) commit transaction
Redirected to http://localhost:3000/projects/7
Completed 302 Found in 7ms (ActiveRecord: 1.3ms)

我的项目/show.html.erb

<p id="notice"><%= notice %></p>

<p>
<strong>Project Name:</strong>
<%= @project.name %>
</p>

<p>
<strong>Album Name:</strong>
<%= @project.album.name %>
</p>



<%= link_to 'Edit', edit_project_path(@project) %> |
<%= link_to 'Back', projects_path %>

当我创建项目/新建时不起作用,但如果我创建相册/新建相册,则相册获得一个名称。


编辑(解决方案)

我的代码在数据库中插入了正确的东西,但我无法在我的项目显示 View 中显示。所以,为了工作,我的 projects_controller 看起来像这样:

  def show
@project = Project.find(params[:id])
@album = Album.take
end

def create
@project = Project.new(project_params)
end

为了在 project.find 解析的 View 中显示项目 :name,我发现 Album.take here它可以在同一 View 中显示专辑 :name。

Obs:take 方法在没有任何隐式排序的情况下检索记录。Obs2:显示 View 保持不变。

最佳答案

在您的创建 操作中,您需要执行以下操作:

def create
@project = Project.new project_params
# Don't need to create dependent objects

respond_to do |format|
if @project.save
...

您不需要单独创建相册;所有其他代码看起来应该可以工作。如果没有,您将需要从您的表单中发布提交的参数(我将能够向其写入更新)。

--

更新

要在 View 中显示专辑,您只需要调用关联方法:

#app/controllers/projects_controller.rb
def show
@project = Project.find params[:id]
end

#app/views/projects/show.html.erb
<% @project.albums.each do |album| %>
<%= album.title %>
<% end %>

关于ruby-on-rails - 嵌套对象 has_one 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34955093/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com