Tag: ajax

使用Ruby on Rails的Ajax简单视图刷新

我想知道是否有人可以帮助我解决我在Rails中制作一些AJAX的问题。 我想尝试做一些简单的事情,我只是想不断检查索引视图是否会在屏幕外显示新条目? 每隔几秒钟它就会很酷,新的条目会消失吗? 新条目位于名为image的div类中,其内部是: 我已经尝试了一些教程,但有些似乎已经过时了,我正想要实现的目标。 我希望有人可以帮助我朝着正确的方向前进。

问题回形针与ajax

我正在阅读本教程: http : //sleekd.com/general/adding-multiple-images-to-a-rails-model-with-paperclip/因为我需要在独立模型中保存产品图像。 但是,当Product.create执行时,请不要保存产品图像模型的数据。 注意:我使用新的包操作,因为我需要使用ajax来创建新产品。 我需要帮助。 我的代码 控制器 class Admin::PacksController @pack } end end def create_starred_product product = Product.new(params[:product]) product.save … end 视图 {:controller => “products”, :action => “create_starred_product”}, :html => {:multipart => true} do |f| %> ….#OTHER FIELDS OF PRODUCT. ITS SAVE METHOD IS OK Imágenes Imagen “textarea” -%> 楷模 class Product :pack_products […]

Rails:使用remote:true来停止页面刷新

我有一个projects/show.html.erb页面。 project has_many project_messages并且从projects/show.html.erb页面,用户可以成功创建新的project_message ,但是,当通过_form partial创建新的project_message时,页面将刷新。 我想使用:remote => true将project_messages添加到项目而不必刷新页面。 请参阅下面我使用的代码。 这不起作用,页面仍然刷新。 我是rails的新手,所以任何帮助都将不胜感激。 请参阅下面每个文件的代码 在projects/show.html.erb中显示project_message并创建一个新的project_message,我有以下代码成功: , 在project_messages_controller.rb文件中,create方法如下,我添加了format.js { } def create @project_message = ProjectMessage.new(project_message_params) @project_message.user_id = current_user.id if current_user @project_message.project_id = current_user.team.project_id if current_user respond_to do |format| if @project_message.save format.html { redirect_to @project_message.project } format.json { render :show, status: :created, location: @project_message } format.js { } […]

选择’file_field’文件时检查文件扩展名,并使用Ruby on Rails显示错误消息(如果有)

我正在运行Ruby on Rails 3和Prototype,我想在为file_field选择文件时检查文件扩展名,所以如果它不是允许的那个(我的情况下只有.doc和.pdf ,我可以显示错误.pdf是允许的扩展名)。 此时在我的视图文件中,我以这种方式观察file_field : page.event.observe(‘file_field_id’, ‘change’) do |element| # Here I would like to put some code in order to check the file extension # If the file extension isn’t ‘.doc’ or ‘.pdf’ I would like to set and show an error text in the ‘error_id’ # I know using ‘element[:error_id].show’ I […]

Ajax with Polymorphic Association(Destroy&Create)

我在rails应用程序中与dailyposts和comments有多态关联 一切都在数据库中工作正常,但当我尝试将Ajax添加到Destroy Action(我使用本教程http://railscasts.com/episodes/136-jquery-ajax-revised )….它不起作用除非我刷新页面。 (但我的警报在destroy.js.erb中有效) 我知道我的错误在destroy.js.erb中 新手Rails …请帮忙:) 这是我的代码…… ROUTES resources :dailyposts do resources :comments end CONTROLLERS ##Dailyposts class DailypostsController :created_at) @comment = Comment.new end end ##Comments class CommentsController < ApplicationController before_filter :load_commentable respond_to :html, :js def create @comment = @commentable.comments.create(params[:comment]) @comment.user = current_user if @comment.save respond_to do |format| format.html { redirect_to @commentable } format.js […]

Rails:用户使用以下命令销毁不相关的对象后退出:remote => true

我正在关注http://railscasts.com/episodes/250-authentication-from-scratch进行简单的身份validation。 它按预期工作。 我的应用程序中有一个模型,其中包含以下partial : ‘Are you sure?’, :method => :delete, :remote => true %> 它在index.html.erb调用如下: @posts.reverse %> destroy.js.erb如下所示,如果对象被成功销毁则调用它。 $(‘#’).css(‘background’, ‘red’); $(‘#’).hide(); 单击delete按钮后, post对象将被正确删除, destroy.js.erb也会正确呈现。 但不知何故,用户已注销。 以下是我的posts_controller.rb的代码: def destroy logger.error ‘in destroy’ @post = Job.find(params[:id]) @post.destroy respond_to do |format| format.html { redirect_to(posts_url) } format.xml { head :ok } format.js end end 任何线索为什么会出现这种行为? 并且,如果我从delete链接中删除:remote => true ,那么用户仍然登录。我在session的destroy方法中有日志语句,在任何一种情况下都不会被调用,但如果’ […]

在轨道上的ruby中的Ajax flash消息

我正在尝试显示一条Flash消息并使用Ajax呈现它,但是在我刷新页面之前 ,消息似乎没有出现。 这是我的create.rjs文件: page.insert_html :top, :feed_items, :partial => ‘shared/feed_item’, :object => @micropost page.replace_html :user_info, pluralize(current_user.microposts.count, “micropost”) page[:micropost_form].reset page.replace_html :notice, flash[:notice] flash.discard 这是我的应用程序布局视图的相关部分: <div class="flash “> 这是我的微控制器的相关部分: class MicropostsController [:create, :destroy] before_filter :authorized_user, :only => :destroy def create @micropost = current_user.microposts.build(params[:micropost]) respond_to do |format| if @micropost.save flash[:success] = “Micropost created!” format.html { redirect_to root_path } format.js […]

如何使用“link_to”而不是“checkbox”进行POST?

这个想法是,当用户有missed_day (又名罢工)时,他会点击此按钮。 他目前可以点击一个复选框来标记missed_day 。 习惯/节目 <label id="” class=”habit-id”>Strikes: <label id="” class=”habit-id-two”>Strikes: = (index + 1) %> <p data-submit-url="” data-delete-url=””> <label id="” class=”level-id”>Level : <label id="” class=”level-id-two”>Level : 0, {class: “habit-check”} %> 1, {class: “habit-check”} %> 2, {class: “habit-check”} %> habit.js $(document).ready(function() { $(“.habit-check”).change(function() { var submitUrl = $(this).parents(“p”).data(“submit-url”); var deleteUrl = $(this).parents(“p”).data(“delete-url”); if($(this).is(“:checked”)) { $.ajax( […]

Ajaxified Star Rating System未正确更新

我使用本教程实施了星级评分系统 http://eighty-b.tumblr.com/post/1569674815/creating-an-ajaxified-star-rating-system-in-rails-3 Ajax完美运行,直到我添加Javascript,提交表单/在给定用户进行更改后保存数据。 出于某种原因,它将遍历我的索引页面上的所有元素,并在所选对象上提交正确的整数值,但随后在其余部分上提交一堆NIL值。 我只希望它更新那个ONE对象。 ( 如何在Javascript中提交相应的图书ID? ) 新的铁路请帮助:) VIEWS index.hrml.erb(书籍) <table id="book”> #####This is my partial for my form ‘ratings/rating’, :locals =>{:book => book} %> _rating.html.erb Avg. Rating { :class => ‘rating_ballot’ }, :remote => true do |f| %> “rating”, :id=>”1″}) %> ‘rating_button’) %> “rating”, :id=>”2″}) %> ‘rating_button’) %> “rating”, :id=>”3″}) %> ‘rating_button’) %> […]

Ruby rails – jquery ajax提交表单

发送ajax请求是否正确? 此代码无效,我需要在此处更改哪些内容? 有没有更好的方式发送ajax表单? true, :id => ‘create_item’ do %> 40, :size => 70 %> 10, :size => 10, :value => Time.now.utc.strftime(“%Y-%m-%d”) %> 10, :size => 10, :value => Time.now.localtime.strftime(“%H:%M:%S”) %> 50, :size => 50 %> “new” %> “javascript:submitForm()” %> function submitForm() { $.ajax({type:’POST’, url: ‘/item/create’, data:$(‘#create_item’).serialize(), success: function(response) { $(‘#create_item’).find(‘#item’).html(response); }}); return false; }