samedi 1 août 2015

Rails form_for populates model with Nil

I am having trouble populating my model with my parameter passed from form_for as it assigns Nil to the id, my column (decrypted), and created/updated at fields.

Below is my view (new.html.erb):

  <%= form_for @decrypt_text, url: { action: "create" }  do |f| %>  
  <div class="field"> 
    <%= f.label :decrypt_text %><br> 
    <%= f.text_area :decrypted %><br> 
  </div> 
  <div class="actions"> 
    <%= f.submit "Submit" %> 
  </div> 
 <% end %>

and my controller:

class DecryptController < ApplicationController
    def new
        @decrypt_text = Dmessage.new
  end

  def create
      @decrypt_text = Dmessage.new(params[:decrypted]) 
      p @decrypt_text

      if @decrypt_text.save 
        redirect_to '/decrypt/display' 
      else 
        redirect_to '/' 
      end     
  end

  def display
    @displayme = Dmessage.order("created_at").last
  end

end

and just in case, my model:

class CreateDmessages < ActiveRecord::Migration
  def change
    create_table :dmessages do |t|
      t.text :decrypted
      t.timestamps null: false
    end
  end
end

I know it's assigning nil values because this:

 p @decrypt_text

prints out:

#<Dmessage id: nil, decrypted: nil, created_at: nil, updated_at: nil>

I really am not sure what I am missing but I am fairly new to Rails. Any help is appreciated!

Aucun commentaire:

Enregistrer un commentaire