Reputation: 219
I've got a very simple app. All this app needs to do is check if a username exists. If if does, redirect to the user. If it does not exist, create a new user as per the username entered and redirect to the new user. New users never get created for some reason but the find_or_create line works in rails console. Here is the code:
user_controller.rb
def new
@user = User.new
end
def create
@u = User.find_or_create_by_name(params[:name])
redirect_to @u
end
new.html.erb
<% form_for(@user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :Username %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.submit 'Submit' %>
</p>
<% end %>
Any help would be appreciated.
Upvotes: 2
Views: 553
Reputation: 12165
Rails is going to package the whole form within params[:user]
, so you really want params[:user][:name]
That is:
@u = User.find_or_create_by_name(params[:user][:name])
Upvotes: 7