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

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

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.

This one is Rails 4 specific

http://edgeguides.rubyonrails.org/getting_started.html

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

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:

<!DOCTYPE html> <html> <head>   <title>Blog</title>   <link data-turbolinks-track="true" href="/assets/application.css? body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/posts.css?body=1" media="all" rel="stylesheet" /> <link data-turbolinks-track="true" href="/assets/welcome.css?body=1" media="all" rel="stylesheet" />   <script data-turbolinks-track="true" src="/assets/jquery.js? body=1"></script> <script data-turbolinks-track="true" src="/assets/jquery_ujs.js? body=1"></script> <script data-turbolinks-track="true" src="/assets/turbolinks.js? body=1"></script> <script data-turbolinks-track="true" src="/assets/posts.js?body=1"></

<script data-turbolinks-track="true" src="/assets/welcome.js?body=1"></

<script data-turbolinks-track="true" src="/assets/application.js? body=1"></script>   <meta content="authenticity_token" name="csrf-param" /> <meta content="9TL7qWKSPp2+gCFUgstifTB3cyZs/Q2whYQGIXDI5n0=" name="csrf-token" /> </head> <body>

</body> </html>

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

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

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

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.

new.html.erb copied from the tutorial as is: <% form_for :post, url: posts_path do |f| %>   <p>     <%= f.label :title %><br>     <%= f.text_field :title %>   </p>

  <p>     <%= f.label :text %><br>     <%= f.text_area :text %>   </p>

  <p>     <%= f.submit %>   </p> <% 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.

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

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

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

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