Search code examples
ruby-on-rails-3formsactiveresource

ActiveResource, a model, and Form_Tag


I am trying to use form_tag to pass the params captured by the form to my users controller. I am attempting to communicate with a Sinatra server, and so I do not have a database on the client. My view is as follows:

 <% form_tag(@user) do %>

<div class="field">
<%= label_tag :first_mame %><br />
<%= text_field_tag :first_name  %>
</div>
<div class="field">
<%= label_tag :last_name %><br />
<%= text_field_tag :last_name %>
</div>
<div class="field">
<%= label_tag :email %><br />
<%= text_field_tag "user[email]" %>
</div>
<div class="field">
  <%= label_tag :device_id %><br />
  <%= text_field_tag "user[device_id]" %>
</div>
<div class="field">
<%= label_tag :type %><br />
<%= text_field_tag "user[device_type]" %>
</div>
<div class="actions">
<%= submit_tag %>
 </div>
<% end %>

The create action on my controller is simply:

 def create
 @user = User.new(params[@user])
 @user.save

respond_to do |format|
  if @user.save
    format.html { redirect_to(@user, :notice => 'User was successfully created.') }
    format.json {render :json => @user }
    format.xml  { render :xml => @user, :status => :created, :location => @user }
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
  end
end

end

Here's what I get as a result => expected an attributes Hash, got nil

Anybody know why? Thanks for the help.


Solution

    1. You need to use form_for and not form_tag. form_for(@user) do
    2. In your model you need to create a schema. Without it Rails doesn't know what do with the data you enter into the form.
    3. When you pass the object into the parameter hash use :user and not @user. @user = User.new(params[:user])