Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular.js integration with Ruby On Rails Forms

Using angular.js often items like ng-click or ng-model are written directly in the html form element like so....

<input type="text" ng-model="name">

How would I do that with rails? As rails uses embedded ruby and generates the html....

<%= form_for(@user) do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>

How would I add the ng-model to <%= f.text_field :name %> ?

like image 931
HelloWorld Avatar asked Jun 19 '12 17:06

HelloWorld


2 Answers

Ideally, you don't want to be mixing embedded ruby interpolation and Angular's interpolation. It's better to have ruby asynchronously serve JSON to Angular, and let Angular take care of filling in the data on the client side.

like image 177
btford Avatar answered Oct 10 '22 02:10

btford


Try this, when it's a hyphen separated word, you need to put within a hash notation.

f.text_field :name, :ng => {:model => "name"}
like image 44
MurifoX Avatar answered Oct 10 '22 02:10

MurifoX