user2284821
user2284821

Reputation: 431

How can I submit form content to a Rails app from an outside static html page?

I have an html form on an outside legacy[coldfusion] system that i would like to submit to the create method in my users controller in my new rails app- unfortunately i'm a rails noob and clueless.

the html in the outside legacy app is:

<form action="http://floating-caverns-7335.herokuapp.com/users" method= "post">
<input type="text" name="name" value="nametestvalue">
<input type="submit" name="Submit" value="testROR">

the code in the rails app is the [standard noob scaffolded] method for create in the users controller:

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

when i execute the first html code a new user record is created by the rails app but the value of the name field is blank. what am i overlooking that will correctly load the name field value as specified?

Upvotes: 0

Views: 710

Answers (2)

user645579
user645579

Reputation:

You can see that a new user is being created with params[:user]. So, you need to send an user hash like that :user => { name: "an user"} to the server, and you do that changing the input name attribute:

<input type="text" name="user[name]" value="nametestvalue">

Upvotes: 1

maximus ツ
maximus ツ

Reputation: 8065

Just replace this line,

<input type="text" name="user[name]" value="nametestvalue">

This is because in following statement

@user = User.new(params[:user])

you are passing params[:user] which is currently blank. This need to contain user attributes. I will suggest you to inspect web logs to check parameter list. You can do the same in firebug net panel as well.

Hope this helps!

Upvotes: 0

Related Questions