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’ %>

Title
<%= text_field 'page', 'title' %>

Created at
<%= datetime_select 'page', 'created_at' %>

Content
<%= text_area 'page', 'content' %>

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?

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’ %>

Title
<%= text_field 'page', 'title' %>

Created at
<%= datetime_select 'page', 'created_at' %>

Content
<%= text_area 'page', 'content' %>

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.

Another thing I just spotted is your use of created_at here…

Created at
<%= datetime_select 'page', 'created_at' %>

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 P.

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 P.

Aurélien Bottazini wrote:

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 P.

Thank you to both of you.

It works perfectly now.

Another thing I just spotted is your use of created_at here…

Created at
<%= datetime_select 'page', 'created_at' %>

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 P.

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:

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.