Reputation: 541
I'm having a small issue with saving form values. I made a form for updating some database values, and I've ran into a peculiar problem.
I created 3 variables for several items, here are the first 3 from the form.
follow_up_logs/edit.html.erb
<div id="followuplog_attribute_update">
<p>
Baseline Target:
<%= f.number_field :baseline_target %>
</p>
<p>
Baseline Completed:
<%= f.number_field :baseline_completed %>
</p>
<p>
Baseline Reasons:
<%= f.text_field :baseline_completed %>
</p>
</div>
And, to show you the pattern, here are the second three:
<div id="followuplog_attribute_update">
<p>
3 Week Target:
<%= f.number_field :week3_target %>
</p>
<p>
3 Week Completed:
<%= f.number_field :week3_completed %>
</p>
<p>
3 Week Reasons:
<%= f.text_field :week3_completed %>
</p>
</div>
Here is how I created them:
add_column :follow_up_logs, :baseline_target, :integer add_column :follow_up_logs, :baseline_completed, :integer add_column :follow_up_logs, :baseline_reasons, :string
add_column :follow_up_logs, :week3_target, :integer add_column :follow_up_logs, :week3_completed, :integer add_column :follow_up_logs, :week3_reasons, :string
The problem is, only the target variable saves. All the rest don't save to the database. So when I submit the form, for some odd reason, :baseline_target and :week3_target both save to the database, however nothing else does. (Well, actually the completed variables save as 0 and the reasons variables are left as nil)
Here are my controller actions:
follow_up_logs_controller.rb
def new
@follow_up_log = FollowUpLog.new
end
def create
@follow_up_log = FollowUpLog.new(params[:follow_up_log])
if @follow_up_log.save
redirect_to @follow_up_log
else
render 'new'
end
end
def update
@follow_up_log = FollowUpLog.find(params[:id])
if @follow_up_log.update_attributes(params[:follow_up_log])
flash[:success] = "Log Updated"
redirect_to @follow_up_log
else
render 'edit'
end
end
def edit
@follow_up_log = FollowUpLog.find(params[:id])
end
I haven't been able to figure out what's going on, any help would be greatly appreciated. I feel like it must be something small I keep missing =(
edit. spelling
Upvotes: 1
Views: 1301
Reputation: 53038
You are repeating the attribute :baseline_completed
.
<p>
Baseline Completed:
<%= f.number_field :baseline_completed %>
</p>
<p>
Baseline Reasons:
<%= f.text_field :baseline_completed %> ## <== should be :baseline_reasons
</p>
Same goes for other form with :week3_completed
That's why when you submit the form the text_field value overrides the number_field value. And your completed variables save as 0 and the reasons variables are left as nil
.
Upvotes: 2