Issues with Rails 3.1 API with Query String to Create action on Mac OSX Mountain Lion
Posted
by
hjaved
on Stack Overflow
See other posts from Stack Overflow
or by hjaved
Published on 2012-06-26T09:12:49Z
Indexed on
2012/06/26
9:15 UTC
Read the original article
Hit count: 391
Hi I've been stuck on this problem for a while and would appreciate your help. I'm writing an API to allow an external source like a Browser Query String or a smartphone to enter some model User info in a form and hit the User create action to write the data to the db. Please tell me what I'm doing wrong with the code below.
I've also observed that if I have code like @user = User.new(params[:user]), that this approach only works when a user enters their data within the form.
And that if I have code such as @user = User.new( name: params[:name], location: params[:location], password = params[:password], email: params[:email]), that this code ONLY works for a Query string entry, but NOT both Query string AND regular form submission.
Why is that and how can I write the code above in the Users Controller Create action, so that it takes care of both situations?
URL used: localhost:3000/users/create?name=John&&[email protected]&&password=secret&&location=SanFrancisco&date=06122012
The date is of type string but it doesn't show up in the database. Why? Everything else does.
UsersController.rb
def create
@user = User.new(params[:user])
if @user.save
session[:uid] = @user.id
redirect_to thanks_path, notice: "Welcome #{@user.name}!"
else
redirect_to root_path
end
end
New User Form:
<%=u.text_field :name, placeholder: "Name"%><br>
<%=u.text_field :email, placeholder: "Email"%><br>
<%=u.password_field :password, placeholder: "Password"%><br>
<%=u.text_field :location, placeholder: "City"%><br>
<%=u.text_field :date, placeholder: "Date"%><br>
<%if params[:partner_id]%>
<%=u.hidden_field :partner_id, value: params[:partner_id]%>
<%end%>
<button class="btn btn-large btn-primary">Enter</button>
I also tried to create a separate method called remotecreate for User creation for something other than a regular web form. I entered remotecreate in the Query string but it didn't work.
def remotecreate @user = User.create(name: params[:name], email: params[:email], password: params[:password], location: params[:location], date: params[:date]) if @user.save session[:uid] = @user.id redirect_to thanks_path, notice: "Welcome #{@user.name}" else redirect_to root_path end end
Thanks!
© Stack Overflow or respective owner