Getting started w/ Rails 4

Hi. I’m trying to run Getting started tutorial under Rails 4.0.0.beta1
although it assumes it runs on Rails 3. I was able to get rid of all
errors by wrapping post_params here and there, and installing
protected_attributes Gem for attr_accessible to make sense. Post/
comment CRUD works just fine, but I don’t seem to be able to add Tags
(the final part of the tutorial). Post saved successfully message
appears, but tags aren’t saved as part of saving the post. Database
isn’t even UPDATEd unless I change either name, title or content, but
tags aren’t persisted in any case. What gives?

The tutorial: Getting Started with Rails — Ruby on Rails Guides

On 4 May 2013 18:37, rihad [email protected] wrote:

Hi. I’m trying to run Getting started tutorial under Rails 4.0.0.beta1
although it assumes it runs on Rails 3. I was able to get rid of all
errors by wrapping post_params here and there, and installing
protected_attributes Gem for attr_accessible to make sense. Post/
comment CRUD works just fine, but I don’t seem to be able to add Tags
(the final part of the tutorial). Post saved successfully message
appears, but tags aren’t saved as part of saving the post. Database
isn’t even UPDATEd unless I change either name, title or content, but
tags aren’t persisted in any case. What gives?

You would be better to stick to Rails 3 (with a Rails 3 tutorial)
until you have mastered the basics of Rails and know how to debug your
code. If you really must go with Rails 4 then railstutorial.org have
a beta tutorial at
http://ruby.railstutorial.org/ruby-on-rails-tutorial-book?version=4.0#top

Using a tutorial aimed at a different version of Rails to that which
you are using is a doomed enterprise.

Colin

This one is Rails 4 specific

Thanks, Scott, I somehow missed the link to edgeguides.* vs. guides.*.

Thanks for the link. I’m learning Rails in my spare time and have no
immediate need for it, so I thought I’d just go ahead and start with
version 4.

On May 4, 11:53pm, Scott E. [email protected] wrote:

This one is Rails 4 specific

Getting Started with Rails — Ruby on Rails Guides

Alas, in “5.2 The first form” the empty form isn’t shown in
new.html.erb.

This is all I get after clicking view source in Firefox:

Blog

On 5 May 2013 16:09, rihad [email protected] wrote:

On May 4, 11:53 pm, Scott E. [email protected] wrote:

This one is Rails 4 specific

Getting Started with Rails — Ruby on Rails Guides

Alas, in “5.2 The first form” the empty form isn’t shown in
new.html.erb.

Are there any error messages in the server window?

Post your app/views/posts/new.html.erb

Colin

Hi, Colin, no errors in the “rails server” window:
Started GET “/posts/new” for 192.168.0.1 at 2013-05-05 20:23:21 +0500
Processing by PostsController#new as HTML
Rendered posts/new.html.erb within layouts/application (11.6ms)
Completed 200 OK in 34ms (Views: 31.2ms | ActiveRecord: 0.0ms)

(and a few more “Started GET /assets/*” lines)

Absolutely same lines in development.log as in server’s output window.

On 5 May 2013 16:17, Colin L. [email protected] wrote:

Post your app/views/posts/new.html.erb

Also look in log/development.log and see if there are any clues. Take
a bit of time to understand what you are seeing there.

Colin

new.html.erb copied from the tutorial as is:
<% form_for :post, url: posts_path do |f| %>

<%= f.label :title %>
<%= f.text_field :title %>

<%= f.label :text %>
<%= f.text_area :text %>

<%= f.submit %>

<% end %>

I can put <%= “hi” %> before or after form_for, and see it. But not
inside form_for - then I won’t see it. Probably because :post is
nonexistent it doesn’t get rendered at all.

On 5 May 2013 16:28, rihad [email protected] wrote:

<%= f.submit %>

<% end %>

I can put <%= “hi” %> before or after form_for, and see it. But not
inside form_for - then I won’t see it. Probably because :post is
nonexistent it doesn’t get rendered at all.

It seems a bit odd doing this before the model has been generated and
the database migrated. I am a bit dubious about whether this guide
works as shown. I suggest trying the one at railstutorial.org that I
mentioned earlier.

Colin

Thanks, will do that for now. I just thought the guides hosted on
rubyonrails.org were more “official” and up-to-date. Well, they
weren’t.

On May 5, 2013, at 12:15 PM, rihad wrote:

Thanks, will do that for now. I just thought the guides hosted on
rubyonrails.org were more “official” and up-to-date. Well, they
weren’t.

They are official for 3.2, which is “stable” until 4.0 clears release
candidate stage. I am glad to see you are trying the new hotness, that’s
how the bugs get found!

Walter

On 5 May 2013 17:33, Hassan S. [email protected]
wrote:

On Sun, May 5, 2013 at 8:28 AM, rihad [email protected] wrote:

new.html.erb copied from the tutorial as is:
<% form_for :post, url: posts_path do |f| %>

Then the tutorial is in error: you need <%= form_for …

Well spotted, I missed that one. The tutorial does have <%= in fact.
It appears to be the copying that is in error.

Colin

On Sun, May 5, 2013 at 8:28 AM, rihad [email protected] wrote:

new.html.erb copied from the tutorial as is:
<% form_for :post, url: posts_path do |f| %>

Then the tutorial is in error: you need <%= form_for …


Hassan S. ------------------------ [email protected]

twitter: @hassan

On May 5, 9:33pm, Hassan S. [email protected]
wrote:

On Sun, May 5, 2013 at 8:28 AM, rihad [email protected] wrote:

new.html.erb copied from the tutorial as is:
<% form_for :post, url: posts_path do |f| %>

Then the tutorial is in error: you need <%= form_for …

Woops, sorry, guys, I read somewhere that it’s better to retype code
than just copy&paste it, poor me :frowning:

This time there does seem to be an obsoleted part not in sync with
latest code on Getting Started with Rails — Ruby on Rails Guides
5.6 Saving data in the controller

@post = Post.new(params[:post])
triggers the exception
ActiveModel::ForbiddenAttributesError in PostsController#create

the line should instead be
@post = Post.new(params.require(:post).permit(:title, :text))
or something to that effect.

p.s. I wonder if this repetitiveness is needed here. For the simple
case such as Post.new(params[:post]), I guess Rails should just “know”
what fields comprise a model and permit relevant safe fields coming
from POSTed data. Of course one can always override that with the
second permit variant, but not be forced to do so, introducing
possibility for error (dreaded DRY).

On 5 May 2013 19:33, rihad [email protected] wrote:

p.s. I wonder if this repetitiveness is needed here. For the simple
case such as Post.new(params[:post]), I guess Rails should just “know”
what fields comprise a model and permit relevant safe fields coming
from POSTed data. Of course one can always override that with the
second permit variant, but not be forced to do so, introducing
possibility for error (dreaded DRY).

I believe you can still use attr_accessible in the model if you want to.
This feature is a change for Rails 4 that gives greater flexibility in
that it allows permitting or require parameters from within the
controller. Why would it not be DRY?

By the way, could you not top post please, it makes it difficult to
follow the thread. Insert your replies inline at appropriate points
in the previous message. Thanks.

Colin