是否可以在部分中仅放置一个 Rails 表单元素?

发布于 2024-07-17 05:13:38 字数 629 浏览 7 评论 0原文

我的应用程序有一个选择框供用户选择“地点”。 正如您所期望的,该选择框是一个表单。 我还在页面上的某处执行了通过 AJAX 创建新场地的操作。 创建新场地后,我想更新场地选择框以反映这一点。

我的解决方案是将选择框放入部分中,并通过控制器中的创建操作渲染部分。

 <div id="venue_select" style="clear: both;">
    <%= render :partial => 'venue/venue_select_box' %>
 </div>

部分看起来像这样:

<%= f.collection_select :venue_id, @user_venues, :id, :name, :prompt => 'Select a venue' %>

其中 f 是表单引用:

<% form_for :shows do |f| %>

问题是 f 在部分中未定义,所以我收到错误。 一种解决方案是包含整个表单,但我觉得这应该没有必要,因为我没有更新整个表单。 关于如何解决这个问题有什么想法吗?

My app has a select box for users to choose a "venue". This select box is, as you would expect, in a form. I also have an action somewhere on the page that creates a new venue via AJAX. After the new venue is created, I would like to updated the venue select box to reflect this.

My solution was to put the select box in a partial and render the partial from the create action in the controller.

 <div id="venue_select" style="clear: both;">
    <%= render :partial => 'venue/venue_select_box' %>
 </div>

The partial looks like this:

<%= f.collection_select :venue_id, @user_venues, :id, :name, :prompt => 'Select a venue' %>

where f is the form reference:

<% form_for :shows do |f| %>

The problem is that f is undefined in the partial, so I get an error. One solution would be to include the entire form, but I feel like that should not be necessary because I am not updating the entire form. Any ideas on how to go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

变身佩奇 2024-07-24 05:13:38

您可以像这样将变量传递给部分:

  <%= render :partial => 'venue/venue_select_box', :locals => { :f => f } %>

对于这样的东西,查看 文档

编辑:要将部分与 AJAX 请求一起使用,您需要在 .rjs 模板中“重新创建”表单。 因此,在您的控制器中,您需要再次找到该对象。

 def venue_select
    @venue = Venue.find(params[:id])
    respond_to do |format|
      format.html
      format.js
    end
 end

然后在你的venue/venue_select.rjs文件中:

 form_for @venue do |f|
   page[:element].replace_html :partial => 'venue/venue_select_box', :locals => { :f => f }
 end

其中:element是你想要替换的选择菜单的id。
基本上,您只需重新创建 form_for ,然后使用它来更新选择字段。

You can pass a variable to the partial like this:

  <%= render :partial => 'venue/venue_select_box', :locals => { :f => f } %>

For stuff like this it is always good to look at the documentation.

EDIT: To use the partial with AJAX request you'll need to "recreate" the form in your .rjs template. So in your controller you'll need to find the object again.

 def venue_select
    @venue = Venue.find(params[:id])
    respond_to do |format|
      format.html
      format.js
    end
 end

Then in your venue/venue_select.rjs file:

 form_for @venue do |f|
   page[:element].replace_html :partial => 'venue/venue_select_box', :locals => { :f => f }
 end

Where :element is the id of the select menu you want to replace.
Basically you just recreate the form_for and then use that to update the select field.

怪我鬧 2024-07-24 05:13:38

使用 Javascript 将地点添加到选择框可能会更容易,作为 AJAX 添加上的 onComplete 挂钩。

It might be easier to add the venue to the selectbox with Javascript, as an onComplete hook on your AJAX add.

隱形的亼 2024-07-24 05:13:38

我同意 Sarah Mei 的观点,如果有任何事情让您的 AJAX 调用返回已添加地点的 JSON 表示,然后创建一个新选项并将其添加到 select 元素。

添加选择选项的一些通用 JS 代码:

var newOption = new Option("Text", "Value");
selectElement.options[selectElement.options.length] = newOption;

I agree with Sarah Mei on this, if anything have your AJAX call return a JSON representation of the venue that was added and then create a new option and add it to the select element.

Some general JS code for adding a select option:

var newOption = new Option("Text", "Value");
selectElement.options[selectElement.options.length] = newOption;
断舍离 2024-07-24 05:13:38

这些都是不错的选择,但我认为我找到了最简单的。 基本上,我只是在 collection_select 中硬编码了名称,所以我不需要“f”变量:

<%= collection_select 'shows[venue_id]', :venue_id, @user_venues, :id, :name, { :prompt => 'Select one of your previous venues' } %>

然后我的 VenueController 如下:

class VenueController < ApplicationController
  layout 'main'
  before_filter :login_required, :get_user

  def create
    begin
      venue = @user.venues.create(params[:venue])
      @user_venues = @user.venues
      render :partial => 'venue_select_box', :success => true, :status => :ok
    rescue ActiveRecord::RecordInvalid => invalid
      flash[:errors] = invalid.record.errors
      render :text => '', :success => false, :status => :unprocessable_entity
    end
  end

end

如果由于任何原因此方法是不好的做法,请告诉我,我会很高兴地相信你答案。

These are all great options but I think I found the easiest. Basically, I just hard coded the name in the collection_select so I would not need the "f" variable:

<%= collection_select 'shows[venue_id]', :venue_id, @user_venues, :id, :name, { :prompt => 'Select one of your previous venues' } %>

Then my VenueController is as follows:

class VenueController < ApplicationController
  layout 'main'
  before_filter :login_required, :get_user

  def create
    begin
      venue = @user.venues.create(params[:venue])
      @user_venues = @user.venues
      render :partial => 'venue_select_box', :success => true, :status => :ok
    rescue ActiveRecord::RecordInvalid => invalid
      flash[:errors] = invalid.record.errors
      render :text => '', :success => false, :status => :unprocessable_entity
    end
  end

end

If this method is bad practice for any reason, please let me know and I will happily credit you for the answer.

池木 2024-07-24 05:13:38

我认为您的解决方案还不错,但是如果您尝试以另一种形式使用此部分,则生成的字段名称会出现问题。

为了避免这种情况,您可以在 ajax 调用中提交 f.object_name 的内容。 在您的情况下,它将是“shows”,并且因为该方法是:venue_id,所以生成的名称将是“shows[venue_id]”。

I think that your solution is not bad, but If you try to use this partial in another form you have a problem with the name of the field generated.

To avoid this you can submit within the ajax call the content of f.object_name. In your case it will be "shows" and because the method is :venue_id the generated name will be "shows[venue_id]".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文