0

I'm basically trying to create a Rails form_for but it simply does not turn parameters. I've checked the server log and it receives the parameter but cannot capture on the create function. I'm watching andrewperk's tutorial (http://www.youtube.com/watch?v=unq1yubL6lQ) about create and save methods. What could I possible done wrong?. Here is my Users controller;

 def new
    @user=User.new
  end
  def create
    @user=User.new(params[:user])
    if @user.blank?
      render :new
    else
      @user.save
      render :index
    end
  end

And here is my form;

<h1>Add an available user</h1>

    <%= form_for @user do |a| %>
        <p>
            <%= a.label :name %>
            <%= a.text_field :name %>
        </p>
        <p>
            <%= a.label :surname %>
            <%= a.text_field :surname %>
        </p>
        <p>
            <%= a.submit "Add new available user"%>
        </p>
    <% end %>
1
  • params[:user] is correct Commented Sep 19, 2012 at 20:40

1 Answer 1

2

You should use params[:user] in your controller, because this is how the HTML form fields are named by default (you can check page source).

Also I'd suggest refactoring the create method a bit. It is a convention that you redirect to index page instead of just rendering it after creating a record.

def create
  @user = User.new(params[:user])
  if @user.save
    redirect_to users_path
  else
    render :new
  end
end
Sign up to request clarification or add additional context in comments.

2 Comments

I'm sorry I have mistyped. I already have the user as params[:user]
You could, if you wanted to be super-fancy, use "respond_with" to clean this up even more :)

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.