开发者

Rails - Create record from javascript prompt

开发者 https://www.devze.com 2023-02-15 03:11 出处:网络
Let\'s say I have a product model, and the user selects its category in a combobox. The category has only one field, the name.

Let's say I have a product model, and the user selects its category in a combobox. The category has only one field, the name. I would like to have a "add" button next 开发者_如何转开发to this combo to let users add another category just by typing its name in a javascript prompt dialog, so the category is inserted in the database and the combo is updated.

What's the best way to achieve this ?

Thanks!


so this is what i understand from the question.. you want to create a record in your table when used clicks the add button. And the value of the record should be the name the user gives in the prompt dialog.

you can achieve this by doing this..

1) make an ajax call to the server side when the user enters the name.. if you use jquery, probably something like this..

    $.post("yourcontroller/yourmethod", { name: "value given by user in the prompt"}, function(data) {
       //this is where you update the combo
    });

2) Declare yourcontroller/yourmethod route in the config/routes.rb..

3) Define yourmethod in your controller which reads params[:name] and based on the ORM you are using creates a record in the table. if you are using Sequel ORM, you might do something like this.

YourTable.create(
  :name => params[:name]
)

that should then create a record in your table.

0

精彩评论

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

关注公众号