Tony Ennis
Tony Ennis

Reputation: 11

Rails Form Submits Empty or Old Values

I'm having a weird problem whereby one of my forms in rails is not receiving any of my post data.

My model is called entries, and the routes are nested within the campaigns controller. When submitting a new entry, the entry is saved but all of the values appear blank, and when editing an existing entry and clicking submit, the entry doesn't take any of the new values entered and instead reverts to original values.

My code is as follows

/entries/_form.html.erb

<%= form_for [@campaign, @entry] do |f| %>

 <div class="row">
  <div class="left">
    <%= f.label :name %>
  </div>
  <div class="right">
    <%= f.text_field :name, :class => "textbox" %>
  </div>

</div>
<div class="row tarow">
  <div class="left">
    <%= f.label :content %>
  </div>
  <div class="right">
    <%= f.text_area :content %>
  </div>

</div>


<div class="row tarow">
  <div class="left">
    <%= f.label :description %>
  </div>
  <div class="right">
    <%= f.text_area :description %>
  </div>

</div>


<div class="row">
  <div class="left">
    <%= f.label :provider %>
  </div>
  <div class="right">
    <%= f.text_field :provider, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :image %>
  </div>
  <div class="right">
    <%= f.text_field :image, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :url %>
  </div>
  <div class="right">
    <%= f.text_field :url, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :votes %>
  </div>
  <div class="right">
    <%= f.text_field :votes, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
    <%= f.label :hours  %>
  </div>
  <div class="right">
    <%= f.text_field :hours, :class => "textbox" %>
  </div>

</div>

<div class="row">
  <div class="left">
   &nbsp;
  </div>
  <div class="right">
<%= f.submit %>
  </div>

</div>
<% end %>

entries_controller.rb

def new

@entry = Entry.new

respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @entry }
end
end

def edit
end

# POST /entries
# POST /entries.xml
def create

  @entry = Entry.new(params[:entry])
  @entry.user_id = current_user.id
  @entry.campaign_id = @campaign.id

    respond_to do |format|
     if @entry.save
      format.html { redirect_to(campaign_entry_path(@campaign.url, @entry) , :notice => 'Entry was successfully created.') }
      format.xml  { render :xml => @entry, :status => :created, :location => @entry }
     else
      format.html { render :action => "new" }
      format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
     end
    end

end

def update


respond_to do |format|
  if @entry.update_attributes(params[:entry])
    format.html { redirect_to(campaign_entry_path(@campaign.url, @entry), :notice => 'Entry was successfully updated.') }
    format.xml  { head :ok }
  else
    format.html { render :action => "edit" }
    format.xml  { render :xml => @entry.errors, :status => :unprocessable_entity }
  end
end
end

Upvotes: 0

Views: 814

Answers (2)

Tony Ennis
Tony Ennis

Reputation: 11

Figured out the problem, was something silly like I thought.

Basically i had been messing with my entry.rb file in relation to something else, and had defined attr_reader and attr_accessible when they should not have been. Not sure exactly what goes on behind the scenes but obviously as a result rails was not parsing the posted information.

Hope this helps anyone with a similar problem, cheers for the help

Upvotes: 1

sunkencity
sunkencity

Reputation: 3472

Check your logs, what exactly does the params hash look like? Most likely you don't have a root level key called :entry, maybe it's params[:campaign][:entry] (I haven't used that style of form so I don't know), also, don't you need to fetch the @campaign as well?

Upvotes: 0

Related Questions