1

尝试在没有脚手架的 ROR 中编程。在我打开应用程序后rails s,输入每个必要的字段'new'并进入'show'页面,'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为 common:

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

这是我的战斗控制器:

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

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>

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

4

1 回答 1

3

这是你的问题 -attr_accessor你的战斗模型中的所有 s。

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

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

于 2013-10-22T05:49:46.570 回答