Gandalf StormCrow
Gandalf StormCrow

Reputation: 26202

Ruby on rails binding form to an object

In this guide:

http://guides.rubyonrails.org/v2.3.11/form_helpers.html#binding-a-form-to-an-object

In the section 2.2 Binding a Form to an Object I saw that this:

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

I get form like this :

<form action="/articles/create" method="post" class="nifty_form">
  <input id="article_title" name="article[title]" size="30" type="text" />
  <textarea id="article_body" name="article[body]" cols="60" rows="12"></textarea>
  <input name="commit" type="submit" value="Create" />
</form>

So the controller method create should be executed and @action should be serialized from form to it. So do I need to declare create with some parameters like :

def create(action)
action.save!
end

Or how would I get hold of action object which I sent from form in controller method create

Upvotes: 0

Views: 3954

Answers (4)

Swati Sucharita
Swati Sucharita

Reputation: 64

Can do it through params.

def create
  @article = Article.new(params[:article])
  @article.save!  
  redirect_to :action => :index #or where ever 

rescue ActiveRecord::RecordInvalid => e
  flash[:error] = e.message
  render :action => :new
end

Upvotes: 0

Bachan Smruty
Bachan Smruty

Reputation: 5734

Here, @article is your object for Article model.

<form action="/articles/create" method="post" class="nifty_form">

This form's action is "/articles/create", that means upon submitting the form, all the form-data will be posted to create action of articles controller. Over there, you can catch the form data through params.

So in your create action

def create
    # it will create an object of Article and initializes the attribute for that object
  @article = Article.new(params[:article]) # params[:article] => {:title => 'your-title-on-form', :body => 'your body entered in your form'}

  if @article.save # if your article is being created
    # your code goes here 
  else
    # you can handle the error over here
  end
end

Upvotes: 1

okliv
okliv

Reputation: 3959

to let create method save your object you just need to pass parameters to new object and than save it

def create
 Article.new(params[:article]).save
end

in reality method can be more difficult with redirect respond_to block and so on...

Upvotes: 0

Dylan Markow
Dylan Markow

Reputation: 124419

All of the form values are passed to the method as a hash. The title field is passed as params[:article][:title], body as params[:article][:body], etc.

So in your controller, you would need to create a new Article from these params. Note that you don't pass a parameter to the create method:

def create
  @article = Article.new(params[:article])
  if @article.save
    redirect_to @article
  else
    render 'new'
  end
end

Upvotes: 1

Related Questions