Forum: Ruby on Rails form inputs resetting on sumbit

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Chris S. (Guest)
on 2006-01-15 22:41
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' %> <br />
	  To: <%= date_select 'report', 'date2'  %> <br />
Category: <%= select(:report, :category_id, @categories) %><br />
	  <%= 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
Jules (Guest)
on 2006-01-15 22:52
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
Chris S. (Guest)
on 2006-01-15 23:19
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?
Chris S. (Guest)
on 2006-01-16 00:57
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?
Chris S. (Guest)
on 2006-01-16 01:29
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!
This topic is locked and can not be replied to.