开发者

RoR+ in rjs add value from prompt to table

in js.rjs file i am shwoing prompt with textbox like

page << "var name = prompt('Enter name:'); "

i want to add the value name to the table names

is it possible to do from rjs

if so pl开发者_开发问答ease guide me how to do it..


You shouldn't do this back and forwarding with rjs. You're returning javascript to be executed by the client, and you can't get the value back directly unless you embed another AJAX call into the javascript your return.

A better way to do this would be to use a single AJAX call. Use a remote_form_for with a text_field for the user to enter their name into, then POST it to your controller action and store it in the database in the normal fashion.

Something like:

# In your view
<% remote_form_for :user, :url => { :controller => :users, :action => :create } do |f| %>
  <%= f.text_field :name %>
  <%= f.submit %>
<% end %>

# In your controller
def create
  @user = User.new(params[:user])
  @user.save
  render :update do |page|
    # Return whatever javascript you want here
    page << "alert('Created user')"
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜