Ray-Von-Mice
Ray-Von-Mice

Reputation: 429

Rails 4 erb Ruby returns nothing

Trying to program in ROR without scaffold. After I open the app with rails s, enter every necessary field in 'new' and enter 'show' page, 'show' gives me nothing, like this:

SHOW shows nothing

And here is my 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>

In my battle controller, I defined show as common:

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

Here is my 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

When I view the page source, I cannot find any child-title h1 and li:

page source of show.html.erb

And Here is model:

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

Here is my rake routes result:

    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

And my 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>

Could someone figure out what's going on?

Upvotes: 1

Views: 191

Answers (1)

sevenseacat
sevenseacat

Reputation: 25039

Theres your problem - all the attr_accessors in the your Battle model.

Rails already provides those for you within ActiveRecord, and you've overwritten them, so your values won't be getting saved properly.

Remove them and all should be good.

Upvotes: 3

Related Questions