Forum: Ruby on Rails Form and Symbol

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.
847c9da8d177dd26f815a53c7742b7be?d=identicon&s=25 Aurélien Bottazini (Guest)
on 2007-01-09 16:52
(Received via mailing list)
I am new to ruby on rails. I checked online and on many tutorials but
I can't find an answer to my problem :(

As an exercise I am creating a weblog

I am using a simple form to add a new "page" to my weblog

Form code:
<%= error_messages_for 'page' %>

<!--[form:page]-->
<p><label for="page_title">Title</label><br/>
<%= text_field 'page', 'title'  %></p>

<p><label for="page_created_at">Created at</label><br/>
<%= datetime_select 'page', 'created_at'  %></p>

<p><label for="page_content">Content</label><br/>
<%= text_area 'page', 'content'  %></p>
<!--[eoform:page]-->

and in the controller I use

@page = Page.new(params[:page])

It works but I need to modify the parameters I am sending to Page.new()

I know I can't modify :page as it is a Symbol.
But how can I access the values contained inside :page?

I have tried b = eval(:a.id2name) to get the variable's contents but
id does not work.

Any idea?
85f8b7bbe07bff22e0b38cca818cf692?d=identicon&s=25 Luke Pearce (kule)
on 2007-01-09 19:07
Aurélien Bottazini wrote:
> I am new to ruby on rails. I checked online and on many tutorials but
> I can't find an answer to my problem :(
>
> As an exercise I am creating a weblog
>
> I am using a simple form to add a new "page" to my weblog
>
> Form code:
> <%= error_messages_for 'page' %>
>
> <!--[form:page]-->
> <p><label for="page_title">Title</label><br/>
> <%= text_field 'page', 'title'  %></p>
>
> <p><label for="page_created_at">Created at</label><br/>
> <%= datetime_select 'page', 'created_at'  %></p>
>
> <p><label for="page_content">Content</label><br/>
> <%= text_area 'page', 'content'  %></p>
> <!--[eoform:page]-->
>
> and in the controller I use
>
> @page = Page.new(params[:page])
>
> It works but I need to modify the parameters I am sending to Page.new()
>
> I know I can't modify :page as it is a Symbol.
> But how can I access the values contained inside :page?
>
> I have tried b = eval(:a.id2name) to get the variable's contents but
> id does not work.
>
> Any idea?

You can use either:

params[:page][:content]

if you wanted to edit the values before putting it into the Page object
or

@page.content

If you want to work on the object after initialising it.
19605c7ede229e507e894865308a6d53?d=identicon&s=25 Gustav Paul (Guest)
on 2007-01-09 20:12
(Received via mailing list)
Another thing I just spotted is your use of created_at here...

> <p><label for="page_created_at">Created at</label><br/>
> <%= datetime_select 'page', 'created_at'  %></p>
created_at is automatically set by rails when the @page object is
created. updated_at, its companion, keeps track of when the object was
last saved after modification.

Gustav Paul
19605c7ede229e507e894865308a6d53?d=identicon&s=25 Gustav Paul (Guest)
on 2007-01-09 20:13
(Received via mailing list)
Aurélien Bottazini wrote:
> <!--[form:page]-->
> and in the controller I use
>
> Any idea?
>
>
> >
>
>
Hey

The params hash is a hash of hashes... ie

irb(main):010:0> params = {:page => {:foo => "bar"}}
=> {:page=>{:foo=>"bar"}}
irb(main):011:0> params[:page][:foo]
=> "bar"
irb(main):012:0> params[:page][:foo] = "diff"
=> "diff"
irb(main):013:0> params[:page][:foo]
=> "diff"

So in your example you could say:
...
params[:page][:title] = "Auto Title"
@page = Page.new(params[:page])
...


Goodluck,
Gustav Paul
19605c7ede229e507e894865308a6d53?d=identicon&s=25 Gustav Paul (Guest)
on 2007-01-09 20:21
(Received via mailing list)
Woops!

params isn't necessarily a hash of hashes, the insertwidget_tag helpers
for instance aren't mapped that way...

<%=text_field_tag :name%>

will get passed to the controller as:
params[:name]

Ciao
Gustav Paul
13ea48f056b19349027d2f4a6921a46a?d=identicon&s=25 Jean-François (Guest)
on 2007-01-09 21:23
(Received via mailing list)
Hi Aurélien,

>
>
> It works but I need to modify the parameters I am sending to Page.new()
>
> I know I can't modify :page as it is a Symbol.
> But how can I access the values contained inside :page?

title = params[:page][:title]
content = params[:page][:content]

and so on...

   -- Jean-François.

--
À la renverse.
847c9da8d177dd26f815a53c7742b7be?d=identicon&s=25 Aurélien Bottazini (Guest)
on 2007-01-09 21:51
(Received via mailing list)
Thank you to both of you.

It works perfectly now.

>
> Another thing I just spotted is your use of created_at here...
>
>> <p><label for="page_created_at">Created at</label><br/>
>> <%= datetime_select 'page', 'created_at'  %></p>
> created_at is automatically set by rails when the @page object is
> created. updated_at, its companion, keeps track of when the object was
> last saved after modification.
>
> Gustav Paul

I used scaffold to generate the code initially. But your right
created_at is automatically set by rails.

I am going to remove it from the form ;)
This topic is locked and can not be replied to.