RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Obviously don't forget about fragment caching. This will save a lot of time on rendering and database hits.

Avatar

You can write JOIN clauses that filter the activities which do not have the trackable association. This is more of a database question, than Ruby.

You can still display attributes this way:

  • use params hash to cache attributes you want to display even when tracked object is removed
  • do not actually remove the objects but set their attribute removed to true (more refactoring required)
Avatar

I do like Form Objects, but all examples of it forget the edit action, which increases the complexity of the Form Object and decreases its beauty.

Avatar

Don't forget to wrap up the save! in a transaction.

  ActiveRecord::Base.transaction do
    user.save!
    profile.save!
  end

If something went wrong in the profile.save! the user must be rolledback.

I do like Form Objects, but all examples of it forget the edit action, which increases the complexity of the Form Object and decreases its beauty.

Avatar

Can't verify CSRF token authenticity

Avatar

Hey all I had a question about this, I'm using 3.2 and was wondering how would I implement live preview using the example above?? i have it working where when I enter the new information it reads the markdown

then i enter

jquery
<script type="text/javascript">
$('#design_description').keyup(function () {
    var impt = $(this).val();
    $(".preview").html(impt);
});

</script>

in the bottom of the view.... its listening and showing the words but no markdown syntax is applied... how would I do this?

Avatar

Aha. I had the same problem but I realized that the ASCIIcast doesn't have it while the main code does!

Avatar

This is awesome. The helper class is just fantastic - very flexible and easily extendible.

Thanks Ryan.

Avatar

Ryan, with the advent of Rails4 do you still recommend this as the the best method for implementing a json api? Do you believe certain intended consumption mediums would respond to different techniques? iPhone client full RESTful actions vs simple read consumtion? Thanks in advance!

Avatar

When creating a new product in this example, released at Date comes with four fields. How do I format to get only one input field for date. I do not need the time
Please assist
Pierre

Avatar

Ok, I have it almost working (without redis), but my question is that whenever I get a dropdown from this, if I put my mouse over the results or I press down arrow, the results go away. How can I get it to be able to select one of the results and fill in the rest of the search box?

Avatar

Removed 'patch :update_many' from routes.rb - now things are looking up :)

Avatar

Everything works great but I have one bug and can not understand why it doesnt work. My search script (which works on submit) works only on 2nd submission. First submit reloads the page and second submit sends AJAX request. After that if submit again -> reload, submit again -> ajax. (one after another)
Why it happens?
My application JS:
$(function () {
// Search form.

$('#identities_search').submit(function () {

$.get(this.action, $(this).serialize(), null, 'script');

return false;

});

$('#sortbut a, #sortbutq a, #identities .pagination a, #pagination a').live('click',
function () {
$.getScript(this.href);
return false;
}
);

});

Avatar

Is there a way to not show a feed like "Ryan deleted a question" (Can't show the question title anyway because the record was destroyed) ? I tried doing this by checking if activity.trackable is false but it didn't work.

Avatar

I'm using these steps with Rails 4 and I received this error:

undefined local variable or method `accessible_attributes' for #Class:0x007fbbd2134da0

Avatar

I think this is wrong, it has changed but it gives error using your syntax.

Avatar

Rookie in need of some help: It doesn't seem to matter what I do, I get this error:
.rvm/gems/ruby-1.9.3-p286-falcon/gems/actionpack-4.0.0/lib/action_dispatch/routing/mapper.rb:229:in `default_controller_and_action': missing :controller (ArgumentError)
Bundler complete without problems, rake or rails commands fails with before mentioned or simular.
I checked controllers, and yes; they are all there...
So far I have narrowed it down to my routes.rb, but can't see what the problem is.
Any help?

Avatar

Does someone has an example with localized facebook login page?
The following doesn't work with omniauth-facebook gem

Rails.application.config.middleware.use OmniAuth::Builder do
   provider :facebook,
      'FB_TOKEN',
      'FB_SECRET',
      :authorize_params => {
         :locale => 'tr_TR',
      }
end

it should produce this link

https://www.facebook.com/login.php?{...}&display=page&locale=tr_TR

but produces instead

https://www.facebook.com/login.php?{...}&display=page

Because of that the facebook login page isn't localized

Avatar

Great screencast. Glad to see it has been revised.

However, this solution only works for simple types of forms/associations. It becomes far more complicated (and confusing) to try and create a form like this that allows you to create different types of associations, each with their own set of fields (i.e. TextareaQuestion, MultipleChoiceQuestion, each subclassing a generic Question class). Almost like a polymorphic form builder.

I'm working on trying to create this type of form. It doesn't feel like this type of thing is supported very well by Rails form helpers. I'm having the same difficulty using the simple_form gem.

Essentially, you need to create a drill-down that allows you to select the type of association you want to create, then display the form fields relevant to the type you have selected.

It would be nice to see an example of this. I bet Ryan Bates would have a nice, clean way to accomplish this. =]

Avatar

Yes, Paypal sandbox site has changed. But it is pretty much the same functionality. In case some configured this with the old sandbox and is trying to use it now, getting the error

This invoice has already been paid.

I solved this temporarily by changing

:invoice => id

with

:invoice => id + 1000

in the paypal_url method.

Avatar

@Antoine You might want to try the following...

ruby
params[:user].permit(..., :role_ids => [])

as this is what worked for me.

Avatar

I'm running a VPS on Ubuntu 12.04, the deployment works fine but i need to restart of the server to update the code !
Why ?

Avatar

Hi Ryan,

hope you're enjoying you're well deserved time off. Just thought I should add that the way to ensure you have the most recent version of mongoid would be to write

Gemfile
gem 'mongoid', git: 'https://github.com/mongoid/mongoid.git'

This is particularly important as the gem available from rubygems.org seems to have some problems with Rails 4 whereas the github version works perfectly with it.

Regards,
Chris

Avatar

Hi, How I can do to make sign_in root is the main route from my app?

Avatar

Rack responses are not required to define join, only each. To ensure all responses are handled correctly by the middleware you can change it to:

ruby
response_body = ''
body.each{|bod| response_body << bod }
response_body
Avatar

any one have solution to find area near to the zip_code i try near method but it will give me all the area which has the same zip_code it can not find near zip_code area so any one have solution then reply me... thanx

i use ex:-> @something = SomeThing.near(params[:search])
in params[:search] i found zip_code of the city like (10001)

if any one have solution of this please reply me

Avatar

ya i also face same kind of problem i also try to search area near to zip code but it will result same zip code data
are you having same kind of problem

Avatar

I have a readonly api hosted on heroku. I also built an android client for this api.

What I want is that only my android app should be able to consume this api.

What is the best possible solution I can apply. Do I need ssl certificate?

Thanks in advance!!

Avatar

I would like to know (links) how to keep user logged in info inside cookies safety.

Avatar

Thanks man, your note just saved me a lot of time!

To disable MiniProfiler temporarily, just append ?pp=disable to the URL.

Avatar

I saw that too. My first instinct was to look up whether or not this is some new HTML5 thing, but then I decided that Ryan might actually be human and it is just a mistake.

Avatar

hi,,what are the changes required for this tutoial,me using rails 4.0.0 version

Avatar

Could you put this on GitHub?

Avatar

Where can I find the example of the fnordmetric.rb file? The link is not to the file.

Avatar

one more for episode to be revised

Avatar

Hi

I'm trying to follow the instructions of the video. I executed the commands as required:

rails new store
rails g scaffold product name price:decimal --skip-stylesheets
rake db:migrate
rails g bootstrap:install
rails g bootstrap:themed products -f

of course I also put the twitter-bootstrap-rails gem into the gemfile

but when I run localhost:3000/products I get this (from the terminal where I run the server)

Started GET "/products" for 127.0.0.1 at 2013-06-29 16:06:39 +0800
Processing by ProductsController#index as HTML
Product Load (0.1ms) SELECT "products".* FROM "products"
Rendered products/index.html.erb within layouts/application (18.3ms)
WARN: tilt autoloading 'less' in a non thread-safe way; explicit require 'less' suggested.
Completed 500 Internal Server Error in 69ms

ActionView::Template::Error (cannot load such file -- less
(in /Users/ansonzeall/Desktop/lab/Rails/bootstrapTest/store/app/assets/stylesheets/bootstrap_and_overrides.css.less)):
2:
3:
4: Store
5: <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %>
6: <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
7: <%= csrf_meta_tags %>
8:
app/views/layouts/application.html.erb:5:in `_app_views_layouts_application_html_erb__2649677383696800529_70257843367120'

says cannot load the less file

Avatar

Thanks for the tip :). Easy solution to what I thought might get uglier.

Avatar

Seems line image attachments is a bit undercooked in active admin. Any examples of a good way to to it, including deleting & preview.

Currently im using paperclip & editing the form like this:

f.input :image,
:as => :file,
:hint => f.template.image_tag(f.object.image.url(:medium))

I could keep going and add some JS and a delete button, but it seems like something the framework should administer

Avatar

By checking the docs Action Mailer - Receiving Emails you'll see that no receivers are provided, i.e. nor POP3 or Maildir built-in support.

You need to somehow trigger the ActionMailer recieve method every time an email is received, e.g.

rails runner 'UserMailer.receive(STDIN.read)'

Mailman solves that for you but ActionMailer doesn't, you would need to write your own Maildir Guard plugin and/or a POP3 polling daemon to call UserMailer.receive

Avatar

I figured it out :). I knew it was something easy.

needed to replace:
session[:user_id] = @user.id

with:
cookies[:auth_token] = @user.auth_token

Avatar

Ryan,

Great episode. When a person creates a new account, there is no remember me functionality and his session is not automatically stored after creating a new account.

In episode #270, you showed us how to accomplish this by adding the following code (and adding : session[:user_id] = @user.id)

users_controller.rb
def create
    @user = User.new(params[:user])
    if @user.save
      session[:user_id] = @user.id
      redirect_to root_url, notice: "Thank you for signing up!"
    else
      render "new"
    end

How would we accomplish this being that we are not using the session and we are now using the cookie?

Louis