scaryguy
scaryguy

Reputation: 7960

Empty form data issue at Rails

I'm tired of pulling my hair... Can someone tell me what is going on with this code?

Basicly, I'm just trying to get data from a form. I checked hundred of times my controller, model and view but no chance for making thing work. My debug @program result is always like these eventhough I fill in ALL of fields: BLANK.

 --- !ruby/object:Program attributes:   
id:    
title:    
content:   
active:    
created_at:    
updated_at:

Here are codes:

Controller:

   class ProgramsController < ApplicationController
  def index
    @program = Program.all
  end

  def show
    @program = Program.find(params[:id])
  end

  def create
    @program = Program.new

    if @program.save
      redirect_to 'index'
    else
      render 'new'
    end
  end

  def new 
    @program = Program.new
  end


  def edit
  end

  def destroy
  end
end

Model:

class Program < ActiveRecord::Base
  attr_accessible :active, :content, :title

  validates :title, presence: true
  validates :content, presence: true

end

View:

<html>
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Program Adding</title>
</head>
<body>

    <% if @program.errors.any? %>
    <% @program.errors.full_messages.each do |msg| %>
    <%= msg %>
    <% end %>
    <% end %>

    <%= form_for(@program) do |p|%>
    <%= p.label :title%><br />
    <%= p.text_field :title %><br />

    <%= p.label :content%><br />
    <%= p.text_area :content %><br />

    <%= p.label :active%><br />
    <%= p.text_field :active %><br />

    <%= p.submit %>

    <% end %>

    <%= debug @program %>

</body>
</html>

Thank you

Upvotes: 0

Views: 760

Answers (1)

Daniel Logan
Daniel Logan

Reputation: 54

It would seem that you are not passing in the form values to your create action.

@program = Program.new

Try replacing that line with

@program = Program.new(params[:program])

This is all assuming you are using the standard restful routing.

Cheers.

Upvotes: 2

Related Questions