0

I am new to ruby, trying to follow the official documentation and create a basic form for creating a post:

<%= form_for @post, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
  <%= f.text_field :title %>
  <%= f.text_area :entry, :size => "60x12" %>
  <%= f.submit "Create" %>
<% end %>

The form is successfully adding an entry to the database, but an empty one, I think I must be missing something in my controller? Do I need to pass the variables somehow?

def create
@post = Main.create
end
3
  • Which docs are you following? Commented May 31, 2013 at 9:58
  • Can you post log here? Commented May 31, 2013 at 9:58
  • Which log visnu? This is the doc I am following, specifically if you scroll down to binding a form to an oject guides.rubyonrails.org/form_helpers.html Commented May 31, 2013 at 10:00

3 Answers 3

1

A basic create action can look like this. You first initialize a new post. Depending on if it successfully saves you proceed.

# app/controllers/posts_controller.rb
class PostsController < ActionController::Base
  def create
    @post = Post.new(params[:post])

    if @post.save
      redirect_to @post, notice: 'Post has been created.'
    else
      render :new
    end
  end
end

You can shorten your form.

<%= form_for @post do |f| %>
  <%= f.label :title %>
  <%= f.text_field :title %>

  <%= f.text_area :entry, :size => "60x12" %>

  <%= f.submit %>
<% end %>

You can see excellent example code along these lines when you generate a scaffold, so I would encourage you to try $ rails generate scaffold Post title body:text and learn by example.

Sign up to request clarification or add additional context in comments.

2 Comments

wy wont this work def create @post = Main.create(:title => params[:title] , :entry => params[:entry]) end
Creating in one step will persist your data in the database, too. However, the return value of create is: "The resulting object is returned whether the object was saved successfully to the database or not." To distinguish whether validations have passed and the object has been persisted, you could check for post.persisted? in your create action. However, I think the option presented above is the customary way. Did you try the scaffolding? It's pretty good code to learn from.
0

Submitting a form passes the values entered into that form (along with some other information) to the controller as a hash called "params" - the params will contain a block labelled with the name of the form, in this case "post".

You need to use the post block from params in the creation of the new object.

def create
  @post = Main.new(params[:post])
  if @post.save
    # handles a successful save
  else
    # handles validation failure
  end
end

Comments

0

Try:

@post = Main.new(params[:post])
@post.save

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.