# Form inputs resetting on sumbit

**URL:** <https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527>\
**Category:** Rails\
**Created:** [January 15, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527 "2006-01-15T21:41:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [January 15, 2006, 9:41pm UTC](https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527/1 "2006-01-15T21:41:10Z")

</div>

I searched for forum for something about this but couldn’t find  
anything.

For example I have this in my view: (you enter start/end dates from  
dropdowns, pick a category and a report is generated.)

\<%= start\_form\_tag :action =\> ‘display\_report’ %\>  
From: \<%= date\_select ‘report’, ‘date1’ %\>   
  
To: \<%= date\_select ‘report’, ‘date2’ %\>   
  
Category: \<%= select(:report, :category\_id, @categories) %\>  
  
\<%= submit\_tag ‘Show Report’ %\>  
\<%= end\_form\_tag %\>

When I submit this form (back to the same action) all of the dropdowns  
are reset to default values, instead of what was selected. I haven’t had  
problems when editing a AR object though. These inputs aren’t related to  
an object, is that why?

Thanks,  
Chris

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [January 15, 2006, 9:52pm UTC](https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527/2 "2006-01-15T21:52:34Z")

</div>

Yes, that’s why. You have to save them in an instance variable, and put  
this value in the select (just like you would in PHP).

Please tell me if you have any problems with it.

Jules

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [January 15, 2006, 10:19pm UTC](https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527/3 "2006-01-15T22:19:56Z")

</div>

Jules wrote:

> Yes, that’s why. You have to save them in an instance variable, and put  
> this value in the select (just like you would in PHP).
> 
> Please tell me if you have any problems with it.
> 
> Jules

Hmm, I’m not quite sure on how to do this, can you give me an example?

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [January 15, 2006, 11:57pm UTC](https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527/4 "2006-01-15T23:57:45Z")

</div>

Chris S. wrote:

> Jules wrote:
> 
> > Yes, that’s why. You have to save them in an instance variable, and put  
> > this value in the select (just like you would in PHP).
> > 
> > Please tell me if you have any problems with it.
> > 
> > Jules
> 
> Hmm, I’m not quite sure on how to do this, can you give me an example?

I was able to get the category to change by creating a report object:  
class Report  
attr\_reader :category\_id  
attr\_writer :category\_id

```
def initialize(cat_id)
	@category_id = cat_id
end

```

end

I messed around with trying to get this to work with date\_select, but no  
luck. Can this be done? If so how do I set date1 and date2?

---

<div class="post-metadata">

**Author:** ![yetanothergeek](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/8e8cbc/32.png) [@yetanothergeek](https://www.ruby-forum.com/u/yetanothergeek)\
**Post date:** [January 16, 2006, 12:29am UTC](https://www.ruby-forum.com/t/form-inputs-resetting-on-sumbit/52527/5 "2006-01-16T00:29:07Z")

</div>

Chris S. wrote:

> Chris S. wrote:
> 
> > Jules wrote:
> > 
> > > Yes, that’s why. You have to save them in an instance variable, and put  
> > > this value in the select (just like you would in PHP).
> > > 
> > > Please tell me if you have any problems with it.
> > > 
> > > Jules
> > 
> > Hmm, I’m not quite sure on how to do this, can you give me an example?
> 
> I was able to get the category to change by creating a report object:  
> class Report  
> attr\_reader :category\_id  
> attr\_writer :category\_id
> 
> def initialize(cat\_id)  
> @category\_id = cat\_id  
> end  
> end
> 
> I messed around with trying to get this to work with date\_select, but no  
> luck. Can this be done? If so how do I set date1 and date2?

Ok answering my post yet again, but I found one solution:

class Report  
attr\_reader :category\_id, :date1, :date2  
attr\_writer :category\_id, :date1, :date2

```
def initialize(cat_id, date1_year, date1_month, date1_day, date2_year, 

```

date2\_month, date2\_day)  
@category\_id = cat\_id.to\_i  
@date1 = Date.new(date1\_year.to\_i,date1\_month.to\_i,date1\_day.to\_i)  
@date2 = Date.new(date2\_year.to\_i,date2\_month.to\_i,date2\_day.to\_i)  
end  
end

So basically I had to create this class just so I could save my select  
lists. I supposed I could have also used select\_year / select\_month /  
select\_day without this class by initializing a Date object in the  
controller, but then there would still be the problem of the category.  
If anyone has a better way of doing this let me know!
