Ajax和红宝石与局部变量Rails的? [英] Ajax and Ruby on Rails with local variable?

查看:156
本文介绍了Ajax和红宝石与局部变量Rails的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的不明白如何在Rails的使用Ajax和红宝石。我一定是失去了一些东西简单。

I really don't get how to use Ajax with Ruby on Rails. I must be missing something simple.

我想要做的是要求用户选择一个日期,然后做一个表的文档显示,但只有选择的日期(日期是文件的属性)。

What I want to do is to ask the user to select a date, and then make a table of documents appear, but only with the selected date (Date is an attribute of Document).

我的想法是创建一个局部变量女巫是不是在我的数据库,选定的日期存储在里面,然后在我看来,建立一个环说,例如@ document.where(:日期=日期)。每个..

My idea is to create a local variable witch is not in my database, store the selected date in it, and then create a loop in my view saying for example @document.where(:date = date).each...

应用/控制器/ documents_controller.rb ,我有:

class DocumentsController < ApplicationController

 def information
    @documents = Document.all
    @date = params[:date]

    respond_to do |format|
      format.html # index.html.erb
      format.js{}
      format.json { render json: @documents}
    end
  end

end

而在应用/视图/文件/ _information.js.erb ,我有:

<%= form_tag(document, :remote => true) do %>
  <%= label_tag(:date, "The selected date is:") %>
  <%= text_field_tag(:date) %>

  <%= submit_tag %>

<% end %>

在最后,我在这里用户将自己的最新一个领域,但提交按钮不会做任何事情。 我怎么会错过?

In the end, I have a field where the user puts his date, but the submit button doesn't do anything. What do I miss ?

推荐答案

随着讨论,你需要一个改变你的app.Lets通过步骤之一顺其自然

As discussed you need to change the flow of your app.Lets go through steps one by one

一个。创建输入字段,你选择你的日期字段。你已经有你的形式为

a. create your input field where you are selecting your date field. You already have your form for that

<%= form_tag(you_path_for_information_method, :remote => true) do %>
  <%= label_tag(:date, "The selected date is:") %>
  <%= text_field_tag(:date) %>
  <%= label_tag(:portfolio, "Add portfolio") %>
  <%= text_field_tag(:portfolio) %>
  <%= submit_tag %>

<% end %>

在控制器

def information
  @documents = Document.all
  @date = params[:date]
  @portfolio = Portfolio.find(params[:portfolio])
  respond_to do |format|
    format.js{}
  end
end

在你的information.js.erb你可以有:

In your information.js.erb you can have:

$("#some_id_of_parent").html("<%=j render partial: "your_partial", locals: {portfolio: @portfolio} %>")

这篇关于Ajax和红宝石与局部变量Rails的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆