Form and Symbol

I am new to ruby on rails. I checked online and on many tutorials but I can't find an answer to my problem :frowning:

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?

Hi Aurélien,

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?

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

and so on...

   -- Jean-François.

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 :frowning:

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?

>

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

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

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

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 :wink: