gpt4 book ai didi

ruby-on-rails - Rails 4 erb Ruby 什么都不返回

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:24 25 4
gpt4 key购买 nike

尝试在没有脚手架的情况下在 ROR 中编程。在我用 rails s 打开应用程序后,在 'new' 中输入每个必要的字段并进入 'show' 页面,'show ' 什么也没给我,就像这样:

SHOW shows nothing

这是我的 battles/show.html.erb:

<% provide(:title, @battle.name) %>
<h1><%= @battle.name %></h1>
<div class="">
<ul>
<li><%= @battle.name %></li>
<li><%= @battle.date %></li>
<li><%= @battle.location %></li>
<li><%= @battle.belligerentA %></li>
<li><%= @battle.belligerentB %></li>
<li><%= @battle.strengthA %></li>
<li><%= @battle.strengthB %></li>
<li><%= @battle.casualtiesA %></li>
<li><%= @battle.casualtiesB %></li>
<li><%= @battle.result %></li>
</ul>
</div>

在我的战斗 Controller 中,我将 show 定义为 common:

  def show
@battle = Battle.find(params[:id])
end

这是我的战斗 Controller :

class BattlesController < ApplicationController
def index
@battle = Battle.all
end

def new
@battle = Battle.new
end

def show
@battle = Battle.find(params[:id])
end

def create
@battle = Battle.new(battle_params)
if @battle.save
redirect_to @battle
else
render 'new'
end
end

private

def battle_params
params.require(:battle).permit(:name, :date, :location, :belligerentA, :belligerentB, :strengthA, :strengthB, :casualtiesA, :casualtiesB, :result)
end
end

当我查看页面源代码时,找不到任何子标题 h1li:

page source of show.html.erb

这是模型:

class Battle < ActiveRecord::Base
attr_accessor :name, :date, :location, :belligerentA, :belligerentB, :strengthA, :strengthB, :casualtiesA, :casualtiesB, :result
before_save { self.name = name.downcase }
validates :name, presence: true, uniqueness: { case_sensitive: false }
validates :date, presence: true
validates :location, presence: true
validates :belligerentA, presence: true
validates :belligerentB, presence: true
validates :result, presence: true
end

这是我的 rake routes 结果:

    battles GET    /battles(.:format)          battles#index
POST /battles(.:format) battles#create
new_battle GET /battles/new(.:format) battles#new
edit_battle GET /battles/:id/edit(.:format) battles#edit
battle GET /battles/:id(.:format) battles#show
PATCH /battles/:id(.:format) battles#update
PUT /battles/:id(.:format) battles#update
DELETE /battles/:id(.:format) battles#destroy
root GET / pages#home
contact GET /contact(.:format) pages#contact
about GET /about(.:format) pages#about

还有我的 new.html.erb:

<% provide(:title, 'Adding New Article') %>
<h1>Adding New Article</h1>

<div class="">
<div class="">
<%= form_for(@battle) do |f| %>
<%= render 'shared/error_messages' %>
<%= f.label :name %>
<%= f.text_field :name %>

<%= f.label :date %>
<%= f.text_field :date %>

<%= f.label :location %>
<%= f.text_field :location %>

<%= f.label :belligerentA, 'Belligerent-A' %>
<%= f.text_field :belligerentA %>
VS
<%= f.label :belligerentB, 'Belligerent-B' %>
<%= f.text_field :belligerentB %>

<%= f.label :strengthA, 'Strength-A' %>
<%= f.text_field :strengthA %>
VS
<%= f.label :strengthB, 'Strength-B' %>
<%= f.text_field :strengthB %>

<%= f.label :casualtiesA, 'Casualties & Losses-A' %>
<%= f.text_field :casualtiesA %>
VS
<%= f.label :casualtiesA, 'Casualties & Losses-B' %>
<%= f.text_field :casualtiesB %>

<%= f.label :result %>
<%= f.text_field :result %>

<%= f.submit "Create New Article" %>
<% end %>
</div>
</div>

有人能弄清楚发生了什么吗?

最佳答案

这是您的问题 - 您的 Battle 模型中的所有 attr_accessor

Rails 已经在 ActiveRecord 中为您提供了这些,而您已经覆盖了它们,因此您的值将无法正确保存。

删除它们,一切都应该是好的。

关于ruby-on-rails - Rails 4 erb Ruby 什么都不返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19508492/

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