尝试在没有脚手架的ROR中编程。在我使用rails s
打开应用后,在'new'
中输入所有必填字段并输入'show'
页面,'show'
不会给我任何内容,例如:
这是我的战斗/ 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>
在我的战斗控制器中,我将show
定义为常见:
def show
@battle = Battle.find(params[:id])
end
这是我的battlecontroller:
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
当我查看网页来源时,我找不到任何子标题h1
和li
:
这是模特:
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>
有人能弄明白发生了什么吗?
答案 0 :(得分:3)
你的问题 - 战斗模型中的所有attr_accessor
。
Rails已经在ActiveRecord中为您提供了这些,并且您已经覆盖了它们,因此您的值将无法正确保存。
删除它们,一切都应该是好的。