2015-09-06 41 views
1

將字符串傳遞到一個對象在控制器我是新來的網絡編程。所以我一直在網上搜索一段時間來自行解決這個問題,但似乎沒有人遇到類似的問題。用ajax

在我的程序中,我得到的用戶輸入包含通過我的app/views/booths/new.html.erb文件中的textarea創建的新展臺的管理員,如下所示。

應用/視圖/亭/ new.html.erb:

<% provide(:title, 'Create New Booth') %> 
<h1>Create New Booth</h1> 

<% javascript_include_tag "booths" %> 

<div> 
    <div> 
    <%= form_for(@booth) do |f| %> 
     <%= f.label :booth_name, "Name of the new Booth" %> 
     <%= f.text_field :booth_name %> 

     <%= f.label :booth_description, "Describe your new Booth" %> 
     <%= f.text_field :booth_description %> 

     <%= f.label :important_notice, "Any important notices?" %> 
     <%= f.text_field :important_notice %> 

     <span><br>Admins for this Booth?<br> </span> 
     <textarea id="create_booth_admins_textarea" rows="30" cols="50"> </textarea> 

     <%= f.submit "Create a new booth", :id => "booth_create_submit"%> 
    <% end %> 
    </div> 
</div> 

應用程序/資產/ Javascript角/ booths.js摘錄:

admins = $("textarea#create_booth_admins_textarea").val(); 

$('#booth_create_submit').click(function() { 
    $.ajax({ 
     url: "/booths_create", 
     data: { "booth_admin_emails" : admins}, 
     type: "post", 
     cache: false, 
     success: function() { 
      console.log("ajax successful"); 
      alert("ajax successful"); 
     }, 
     error: fucntion() { 
      console.log("ajax error"); 
      alert("ajax error") 
     } 
    }); 
}); 

應用程序/控制器/ booths_controller.rb:

def new 
     @booth = Booth.new 
     @other_members = [] 
     @booth_admins = [] 
    end 

    def create 
     temp_hash = booth_params 
     @booth = Booth.new(temp_hash.except(:booth_admin_emails)) 
     @booth.admin_id = current_user.id    #fill in the Booth table 

     @booth_admins = temp_hash[:booth_admin_emails] 

     booth_admins = [] 

     if @booth.save 
      #fill in join table 
      BoothUser.create(:user_id => current_user.id, :booth_id => @booth.id, :is_admin => true) #add current user 

      if [email protected]_admins.nil? 
       booth_admins = @booth_admins.split(",") 
      end 

      if !booth_admins.empty? 
       booth_admins.each do |email| 
        BoothUser.create(:user_id => User.where("email=?", email).id, :booth_id => @booth.id, :is_admin => true) 
       end 
      end 

      # just a flash to tell user that new booth was created 
      name = @booth.booth_name 
      flash[:success] = "New Booth #{name} created!" 
      redirect_to '/booths' 
     else 
      render('show') 
     end 
    end 

    private 
    def booth_params 
     params.require(:booth).permit(:booth_name, :booth_description, :important_notice, :booth_admin_emails) 
    end 

用戶和展位是has_many through關聯,其中連接表被稱爲BoothUser ,其中包含booth_id, user_id, is_admin,其中ID是特定用戶的indicies和它們各自的表中的展位和is_admin的信息是一個布爾值檢查所述用戶是否是管理員(具有編輯亭設置的權限)的展位。

問題出現是因爲在我的展位表中,我只聲明瞭展位的創建者,並且沒有跟蹤該展位的給定管理員,但是試圖通過查找連接表來查找該展位的這些管理員booth_id與該展位的索引相匹配,is_admin爲真。

我一直在試圖傳遞什麼是對create_booth_admins_textarea:booth_admin_emails在該控制器的輸入,但到目前爲止沒有運氣,因爲沒有傳遞到:booth_admin_emails。我猜這是因爲:booth_admin_emails不是BoothUserBoothUser模型的屬性。

而我在網絡上找到的一些方法可以通過使用強大的參數(?)允許使用的form_for或AJAX模型的屬性參數。但似乎沒有人通過輸入而不是模型的一個屬性到控制器。

所以,我想問問有沒有辦法這樣做,如果有,我怎麼辦呢?還是隻是不允許?

回答

1

您應該能夠像這樣創建:

BoothUser.create(user: current_user, booth: @booth, is_admin: true) #add current user 
booth_admins.each do |email| 
    admin = User.find_by(email: email) 
    BoothUser.create(user: admin, booth: @booth, is_admin: true) 
end 

在你的HTML,你需要創建textarea的是這樣的:在

<textarea id="create_booth_admins_textarea" name="booth[booth_admin_emails]" rows="30" cols="50"> </textarea> 

那麼你不需要的JavaScript所有。

...但是如果你想整個表格通過AJAX提交,那麼除了HTML的變化上面,在你的JavaScript做到這一點:

$('#booth_create_submit').click(function(e) { 
    e.preventDefault(); # keeps the HTML form from submitting. 
    var the_form = $(this.form); 
    $.ajax({ 
     url: the_form.attr('action'), 
     data: the_form.serialize(), 
     type: "post", 
     cache: false, 
     success: function() { 
      console.log("ajax successful"); 
      alert("ajax successful"); 
     }, 
     error: fucntion() { 
      console.log("ajax error"); 
      alert("ajax error") 
     } 
    }); 
}); 
+0

噢,對不起......我想我還沒有明確提出,什麼也沒有被上到控制器過去了,所以當我嘗試使用我的電子郵件中添加新條目到BoothUser表的:booth_admin_emails場是空的,所以沒有被複制到@booth_admins和booth_admins ....你碰巧知道爲什麼沒有什麼會傳遞到這個領域? – ohnu93

+0

我明白了。這個問題似乎是你的HTML和控制器不一致。我會更新我的答案。 –

+0

您在textarea中缺少名稱屬性,因此它不會與表單一起提交。 –