0

ruby on rails についてはまだかなり新しいものですが、ajax リクエストを簡単に実行できることを読んだことがあります。私がやろうとしているのは、データベース テーブルに新しい行を追加し、そこからいくつかの変数を渡すことです。

私が現在持っているのは

%li= link_to image_tag('trans.gif', :border => 0, :size => '16x16', :class => 'i-16-tick'), availabilities_path(:team_id => schedule.team_id, :user_id => current_user, :schedule_id => schedule.id), :remote => true, :method => :post, :title => 'Accept', :rel => 'tooltip-html'

コントローラーコード

`# POST /availabilities # POST /availabilities.json def create @availability = Availability.new(params[:availability])

respond_to do |format|
  if @availability.save
    format.html { redirect_to @availability, :notice => 'Availability was successfully created.' }
    format.json { render :json => @availability, :status => :created, :location => @availability }
  else
    format.html { render :action => "new" }
    format.json { render :json => @availability.errors, :status => :unprocessable_entity }
  end
end

終了 `

ただし、データベースに渡す変数をどのように知るのでしょうか?

これが現在起こっていることです

Processing by AvailabilitiesController#create as HTML Parameters: {"schedule_id"=>"18", "team_id"=>"1", "user_id"=>"38"} User Load (1.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 38 LIMIT 1 (0.4ms) BEGIN SQL (20.1ms) INSERT INTO "availabilities" ("available", "created_at", "schedule_id", "team_id", "updated_at", "user_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id" [["available", nil], ["created_at", Tue, 18 Sep 2012 23:07:26 EST +10:00], ["schedule_id", nil], ["team_id", nil], ["updated_at", Tue, 18 Sep 2012 23:07:26 EST +10:00], ["user_id", nil]] (10.5ms) COMMIT

4

2 に答える 2

1

サーバーに複数の変数を渡したい場合は、それらを http 要求に含めます。これは、通常のリクエストだけでなく、AJAX リクエストにも当てはまります。例えば:

<%= link_to 'Send data', accept_availabilty( player, :my_variable => value1, :my_variable => value2), :remote => true %>

ただし、より複雑な送信では、単純なリンクではなく、リモート フォームを使用したい場合がよくあります。

于 2012-09-18T12:50:26.077 に答える
0

If you want to pass information to your rails app, you will need to place the

:remote => true

on to the form_for tag, then your controller will be able to pass those params on to your model.

于 2012-09-18T11:55:49.617 に答える