从控制器上使用jQuery和AJAX的Rails将参数传递给JavaScript来渲染视图中的红宝石红宝石、视图、器上、参数

2023-09-11 22:28:31 作者:如鲸向海

我想Ajaxify一个Ruby on Rails应用程序,以避免用户交互后,每个页面的重新加载。

I am trying to Ajaxify a Ruby on Rails application to avoid the reloading of every page after a user interaction.

我现在管理有幕后创建了一个新的想法,但不显示它一旦被创建。

I now manage to have a new idea created behind the scenes but not to display it once it has been created.

下面是我的code:

形式的思想观点:

<div id="new_proposition_form" style="display:none">
    <%= form_for(@new_proposition, :remote => true) do |f| %>
        <%= f.text_field :title, :class => "proposition_input", :id => "new_proposition_input"  %>
        <%= f.hidden_field :created_by %>
        <%= f.hidden_field :father_id %>
        <%= f.hidden_field :procontra %>
        <%= f.hidden_field :created_at %>
    <% end %>
</div>

创建思路控制器动作:

  def create
    @idea = Idea.new(params[:idea])
    @idea.created_at = Time.now

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

create.js.erb的思想观点:

$("<%= escape_javascript render(:file => 'ideas/new_proposition.html.erb') %>").insertBefore('#end_of_propositions_list');

其中end_of_propositions_list是显示的想法列表的唯一标识符

where "end_of_propositions_list" is the unique identifier of the displayed ideas list.

我所挣扎如下:

如果在 new_proposition.html.erb 视图的思想观点是这样的:

if the new_proposition.html.erb view in the ideas views is something like :

<div>
  New proposition added
</div>

以code,它是不是指任何对象或变量是这类作品的,即用户可以看到发生了什么事的确认。因此,我知道,在浏览器发起的远程动作打控制器,它击中了JavaScript的模板,打了一个局部视图。

with a code that is not referring to any object or variable it kind of works, i.e. the user can see a confirmation of what happened. Therefore I know that the remote action initiated in the browser hit the controller which hit the javascript template which hit a partial view.

现在我想新创建的想法出现,而不是,使用部分检索它的属性,并采取格式化的照顾。 我觉得我需要通过新创建的想法的ID从观念控制器被称为format.js再到局部的create.js脚本创建操作,以便它知道什么主意,从数据库中提取

Now I would like the newly created idea to appear instead, using a partial that retrieves its attributes and takes care of the formatting. I feel I need to pass the id of the newly created idea from the create action in the ideas controller to the create.js script that is called by format.js and then to the partial so that it knows what idea to pull from the database.

我不知道如何处理这个参数从创建控制器动作的create.js和局部视图传球和倒很AP preciate你的帮助。

I do not know how to handle this parameter passing from the create controller action to the create.js and to the partial view and would really appreciate your help.

推荐答案

它看起来不错,我...?所以,你想要做什么其次是控制器通过实例变量@idea的部分:

It looks good to me...? So what you want to do next is pass the instance variable @idea from controller to the partial:

$("<%= escape_javascript render(
    :file => 'ideas/new_proposition.html.erb', 
    :idea => @idea) %>")
.insertBefore('#end_of_propositions_list');

然后访问@idea在您的视图:

and then access the @idea in your view:

<div>New proposition added</div>
<div>Title: <%= @idea.title => </div>

希望帮助