Oct
17
2017

Ruby on Rails Tutorial Part 6 – CRUD – Create – Save Method

In this Ruby on Rails Tutorial we’ll learn how to create new posts using using the save method and the form_for method. We’ll also learn about redirecting and flash messages. In this video I’m using Rails 3.1 and Ruby 1.9.2 on Linux.

The source code for this video should be available on my site within a few days: http://www.andrews-custom-web-design.com/
Video Rating: / 5

This Ruby on Rails tutorial explores the syntax for rendering different View templates. Watch more at http://www.lynda.com/Ruby-on-Rails-3-tutorials/essential-training/55960-2.html?utm_medium=viral&utm_source=youtube&utm_campaign=videoupload-55960-0501

This specific tutorial is just a single movie from chapter five of the Ruby on Rails 3 Essential Training course presented by lynda.com author Kevin Skoglund. The complete Ruby on Rails 3 Essential Training course has a total duration of 12 hours and explores the fundamental and best practices for working with the Rails framework

Ruby on Rails 3 Essential Training table of contents:

Introduction
1. What is Ruby on Rails?
2. Installing Ruby and Rails on a Mac
3. Installing Ruby on Rails on a Windows Machine
4. Getting Started
5. Controllers, Views, and Dynamic Content
6. Databases and Migrations
7. Models, ActiveRecord, and ActiveRelation
8. Associations
9. Controllers and CRUD
10. Layouts, Partials, and View Helpers
11. Forms
12. Data Validation
13. User Authentication
14. Improving the Simple CMS
15. Debugging and Error Handling
16. Introducing More Advanced Topics
Conclusion
Video Rating: / 5

13 Comments+ Add Comment

  • This is not related to this tutorial but I just have to say after busting my head over and over and going back and forth on your php with my sql essentials training videos.. I just have to say you are not the best teacher. You lack real world explanations & don't explain in a easy way for people to fully understand what you are talking about. My advice, improve on explaining all this technical mumbo jumbo in real world situations with actual easy to understand examples.

  • Thanks! It's very helpful.

  • Sorry my ignorance, I saw what "render" does, but what "render" means?

  • for those getting the forbidden attributes error

    you'll need to modify:
    @post= Post.new(post_params)

    and you'll need to add:
    def post_params
      allow = [:title, :content]
      params.require(:post).permit(allow)
    end

  • *Attention anybody having trouble with this tutorial in Rails 4.0.X*

    The line where andrew writes:
    <%= form_for @post do |f| %>

    Should be changed to:
    <%= form_for @post,:url => { :action => "create" } do |f| %>

  • How the "Add a New Post" submit button knows how it will work with create method in posts controller ?

  • Because nothing was working i had to make 
       @post= Post.new(params[:post].permit(:title,:content))

    maybe its not good but its working and im just learnig so im leaving this like that 🙂

  • Thanks a lot for those awesome tutorial. It really helped !

  • I keep getting an error on the post_params method that says

    param is missing or the value is empty: post

    any ideas?

  • Damm, almost all the php frameworks copy this methodology!!! :S

  • <%= form_for @post do |f| %>

    needs to be

    <%= form_for @post, :url => { :action => "create" } do |f| %>

  • Hello Andrew Perks, Thank you for taking the time to teach us and share your knowledge. I find this really helpful and i really appreciate it. Im also secretly your biggest fan and in love with you. XOXO

    In 2015, with Rails 4. This worked for me:

    1. For Flash notice in my app/view/layout/application.html.erb
    # insert flash notice in the body before yield

    <body>
    <% if flash[:notice] %>
    <div id= "notice"><%= flash[:notice] %></div>
    <% end %>
    <%= yield %>
    </body>

    2. In the Post Controller located in app/controllers/posts_controller.rb
    # I couldnt do the if else without an error, unexpected end DONT understand whats going on, however, create method – i just redirect users to the Main Page and flashed POST SAVED! We can manage for now.

    class PostsCotroller < ApplicationController
    def create #ActionthenDirect
    @post = Post.new(post_params)
    @post.save
    redirect_to(posts_path, notice: "Post Saved!")
    end

    3. Because Rails 4 now need a more secure parameter, (post_params) is used instead of just the previous parameter. Hence you will have to SCROLL ALL THE WAY DOWN pass the "END" and define what post param is

    class PostsCotroller < ApplicationController
    ….
    end

    # PAST THIS END and add this code below. I tried adding a END for private and got an ERROR LOL! dont know why. Still learning #

    private
    def post_params
    params.require(:post).permit(:title, :content)
    end

    P/s: HOPE THIS WORKS for you. Please let me know how it goes. Im still learning.

  • def create
    @post = Post.create(ad_params)
    if @post.save
    redirect_to posts_path, :notice => "Your post was saved"
    else
    render "new"
    end

    end

    private

    def ad_params
    params.require(:post).permit(:title, :content)
    end