ruby-on-rails-3

Conditional Page Caching [Solution: Conditional Fragment Caching]

China☆狼群 提交于 2020-01-11 05:01:10
问题 Suppose that I have controller home_controller.rb with action index . I want to cache index page so I'm doing: caches_page :index but want it to cache only for users that are not signed in. If I'll make conditional like: caches_page :index, :if => :user_not_signed_in? Page will be cached while first not logged in user comes. Now every logged in user also see not-logged in content. Is there a way to separate this action without changing url? 回答1: cache_if and cache_unless seems to be the

The correct way to handle invalid form submissions in Rails

匆匆过客 提交于 2020-01-11 04:46:25
问题 I'm new to rails and am not sure I agree with the way I've things done in some of the tutorials I've gone through. The issue has to do with how to handle invalid form submissions. The standard way of doing things seem to be: class ThingsController < ApplicationController # POST /things def create @thing = Thing.new(params[:thing]) if @thing.save flash[:notice] = 'Thing created' redirect_to(@thing) else render :action => :new end end When @thing.save fails, the user is presented with the same

Yielding content_for with a block of default content

六眼飞鱼酱① 提交于 2020-01-11 02:39:31
问题 Our Rails projects make heavy use of content_for . However, we quite often need to render default content if nothing is defined using content_for . For readability and maintainability it makes sense for this default content to be in a block. We made a helper method in Rails 2.3 and we've now refactored this for Rails 3 (as below). Both those helpers work very well but I'm wondering if there's a more succinct way I could achieve the same thing in Rails 3. Rails 2.3: def yield_or(name, content

activeadmin heroku stylesheet config issue with partial fix

天涯浪子 提交于 2020-01-11 02:04:27
问题 I was receiving the following error message after precompiling my assets locally and then pushing the code to Heroku: 2012-03-28T17:06:01+00:00 app[web.1]: Started GET "/admin/login" for 67.163.67.203 at 2012-03-28 17:06:01 +0000 2012-03-28T17:06:01+00:00 app[web.1]: 2012-03-28T17:06:01+00:00 app[web.1]: ActionView::Template::Error (File to import not found or unreadable: active_admin/mixins. 2012-03-28T17:06:01+00:00 app[web.1]: Load paths: 2012-03-28T17:06:01+00:00 app[web.1]: /app 2012-03

activeadmin heroku stylesheet config issue with partial fix

依然范特西╮ 提交于 2020-01-11 02:04:08
问题 I was receiving the following error message after precompiling my assets locally and then pushing the code to Heroku: 2012-03-28T17:06:01+00:00 app[web.1]: Started GET "/admin/login" for 67.163.67.203 at 2012-03-28 17:06:01 +0000 2012-03-28T17:06:01+00:00 app[web.1]: 2012-03-28T17:06:01+00:00 app[web.1]: ActionView::Template::Error (File to import not found or unreadable: active_admin/mixins. 2012-03-28T17:06:01+00:00 app[web.1]: Load paths: 2012-03-28T17:06:01+00:00 app[web.1]: /app 2012-03

Prevent double submits in a Rails AJAX form

你。 提交于 2020-01-11 02:03:13
问题 What I have: I have a form that I submit via AJAX. <%= form_for([@map, @annotation], :remote => true ) do |f| %> ... <%= f.submit "Save annotation", :class => "btn btn-primary", :id => "annotation-submit-button" %> <% end %> What I want: I would like to prevent double submits. Since the form only disappears when the request has successfully completed, users can click the submit button as long as the database hasn't finished writing the data. I don't want that, obviously. What I've tried: I

CSS in Rails Asset Path not processed by ERB in development

╄→尐↘猪︶ㄣ 提交于 2020-01-10 20:06:05
问题 I have a Rails app with the following in /app/assets/stylesheets/styles.css.erb : ... #nestedbg { background-position: left top; background-image: url(<%= asset_path 'siteheader2.png' %>); background-repeat: repeat-x; background-attachment: fixed; } ... When I run rake assets:precompile and then run rails s -e production , everything works as expected. However, when I remove the precompiled assets and run rails s in development, the CSS file comes up as shown above instead of being properly

Ruby on Rails, Paperclip: “identify” command working in cmd but not in app

血红的双手。 提交于 2020-01-10 20:00:07
问题 I've installed ImageMagick on my Windows 7 64bit and I have the Paperclip Gem. My User model looks like this: class User < ActiveRecord::Base # Paperclip has_attached_file :photo, :styles => { :thumb=> "100x100#", :small => "150x150>" } end In paperclip.rb and development.rb I have: Paperclip.options[:command_path] = 'C:/Program Files/ImageMagick-6.6.7-Q16' My _form looks like this: <%= form_for(@user, :html => { :multipart => true } ) do |f| %> <% if @user.errors.any? %> <div id="error

How can I update this vote counter dynamically with Ajax/JavaScript (Rails)?

核能气质少年 提交于 2020-01-10 19:55:29
问题 I built a simple voting system: votes_controller.rb: class VotesController < ApplicationController def vote_up @post = Post.find(params[:id]) @vote = @post.votes.create(:user_id => current_user.id, :polarity => 1) end end (If there is any bad practice here, please let me know) views/show.html.erb: <h3><%= @post.votes.count %> votes</h3><br /> <%= link_to "Vote Up", vote_up_path(@post), :remote => true %> (I will put this in a partial of course) routes.rb: get 'votes/:id/vote_up' => 'votes

How best to sanitize fields in ruby on rails

北城余情 提交于 2020-01-10 19:30:07
问题 I currently have a controller capturing some html from TinyMCE on the front end. If I tinker with firebug it is possible to submit script tags and inject alert messages etc on to the screen. edit: Currently I am fixing this in the model by using the sanitize helper: require 'action_view' class NotesController < AuthApplicationController include ActionView::Helpers::SanitizeHelper ... def update params[:note][:content] = sanitize(params[:note][:content], :tags => %w(a object p param h1 h2 h3