Rails:在父模型的视图中创建 has_one 模型?

发布于 2024-12-03 06:52:35 字数 1299 浏览 1 评论 0原文

我有两个模型,角色和背景。角色有一个背景,并且背景属于角色。我设置了一个 _menu 部分来显示在我的角色视图中,以允许用户查看与角色相关的其他模型,例如物品和法术。

但是,Character 与这些其他模型具有 has_many 关系,并且我无法弄清楚如何正确链接到具有 has_one 关系的模型。

这是从我的角色视图页面添加新页面的方法,适用于 has_many 模型:

<%= link_to 'Items Page', character_items_path(@character) unless @character.items.exists? %>

这是菜单部分中的代码,一旦创建页面,它将链接到页面:

<%= link_to 'Items', character_items_path(@character) if @character.items.exists? %>

以及来自我的背景控制器的代码:

def new
  @character = Character.find(params[:character_id])
  @background = @character.build_background(params[:background])
end

def create
  @character = Character.find(params[:character_id])
  @background = @character.create_background(params[:background])
   if @background.save
 redirect_to character_path(@character), :notice => "Background information successfully created!"
   else
 render :action => "new"
 end
end

Any建议?基本上,我希望有一个链接在角色显示页面中创建一个新的背景页面,然后在创建背景后将其显示在菜单部分中,并且当用户单击链接时可以查看和编辑该背景。

我确实尝试像这样编写代码:

<%= link_to 'Background', character_background_path(@character) if @character.background.exists? %>

但是Rails 抱怨.exists?是一个未定义的方法。我猜 .exists 不适用于 has_one 关系,否则我一开始就错误地使用了它。感谢您的任何意见!

I have two models, Character and Background. Character has_one Background, and Background belongs_to Character. I have a _menu partial set up to display in my Character view, to allow users to view other models related to Character, such as Items and Spells.

However, Character has a has_many relationship with these other models and I'm having trouble figuring out how to link correctly to a model with a has_one relationship.

Here's the method for adding a new page from my Character view page, for a has_many model:

<%= link_to 'Items Page', character_items_path(@character) unless @character.items.exists? %>

And here's the code from the menu partial, that will link to a page once a page has been created:

<%= link_to 'Items', character_items_path(@character) if @character.items.exists? %>

And the code from my Backgrounds controller:

def new
  @character = Character.find(params[:character_id])
  @background = @character.build_background(params[:background])
end

def create
  @character = Character.find(params[:character_id])
  @background = @character.create_background(params[:background])
   if @background.save
 redirect_to character_path(@character), :notice => "Background information successfully created!"
   else
 render :action => "new"
 end
end

Any advice? Basically, I want to have a link_to create a new Background page in the Character show page, then have that Background show up in the Menu partial once it's created and able to be viewed and edited when a user clicks on the link.

I did try writing the code like so:

<%= link_to 'Background', character_background_path(@character) if @character.background.exists? %>

But then Rails complains that .exists? is an undefined method. I'm guessing .exists doesn't work for a has_one relationship, or else I'm using it incorrectly in the first place. Thanks for any input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吖咩 2024-12-10 06:52:35

你应该尝试if @character.background。如果未找到背景,则返回 nil(请参阅 http://guides.rubyonrails.org /association_basics.html#has_one-association-reference)

you should try if @character.background. This returns nil if no background is found (see http://guides.rubyonrails.org/association_basics.html#has_one-association-reference)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文