RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

I am using Rails 4, and instead of: rails generate acts_as_taggable_on:migration,
I have to use: rake acts_as_taggable_on_engine:install:migrations.

But I want to remove this rake acts_as_taggable_on_engine:install:migrations that I did, but I can't find anywhere how to do this

Avatar

Did you find a solution to this problem? I am having the same issue with 4.0.2

Avatar

Hey,

Does the example repository in this cast work anymore? I downloaded the code from GitHub and ran the "checklist-after" and the request for a new task reloaded the whole page, not just the form with AJAX.

Cheers

Avatar

How would you do model validation to ensure that user must add at least one field?

Avatar

Please note that for non-US payments payment_fee and payment_gross fields are not populated. You have to take values from mc_fee and mc_gross instead

Avatar

Hi, can you do a more recent episode on devise?

Avatar

i'm getting trouble with the partial.
undefined local variable or method `target' for #<#Class:0x00000003f702d0:0x00000002599460>

the form:

ruby
<%= render "shared/error_messages", :locals => {:target => @freezer} %>
.......

The partial have the same code...

ruby
 <% if  target.errors.any? %>
    <div class="alert fade in alert-error">
.......

but if this code is directly in the form it's working...
but i want to work with the partial...

Avatar

Is this a little dangerous even with receiving a default param from routes table? Is it possible for end user to set ?commentable='arbitrary_class' in a request to create object of any class?

Avatar

Thank you, it was very helpful! Going to try Nested forms gem right now.

Avatar

Great comments, especially Sergey and Adrian's comments helped me get mine working. I put a clear winner button. Here's the code:

raffler.js.coffee

ruby
$scope.clearWinner = ->
    angular.forEach $scope.entries, (entry) ->
      entry.winner = false

index.html.rb

ruby
  <button ng-click="clearWinner()">
    Clear Winners
  </button>
Avatar

I figured out my problem. I had spaces after the commas in my csv file. Once removed, I no longer had the problem.

Avatar

I'm having a problem when I try to import a csv file with more than one column. I get a mass assignment security error in spite of the fact that I have all the attributes listed in my attar_accessible statement. The error seems to list all the attributes except the very first column of my csv file. Any ideas? My code is the same as Ryan's with the exception that I am using a model named "Customer.rb" instead of "Product.rb"

Avatar

Hi, I would you someone can help me.

Im using devise with Rails 4 and im getting an error in my log file trying to sign in.

Unpermitted parameters: password, remember_me

I have this code in my app controller:

class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.

protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?

protected

def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :username) }
end

end

Hope you can help me. Thank you!

Avatar

If you are having a problem with the dynamic add/remove javascript not working I found a solution. The problem for me was the rake gem. My solution was to require rake 0.9.2.2 in gemfile then run "bundle update" to rebuild gems from scratch.

Avatar

Thanks for the suggestion Allen. This fixed my unicorn deploy woes and seems to be pretty clean.

I added these tasks:

ruby
 task :monitor_all, :roles => :app do
    sudo "monit monitor all"
  end

  task :unmonitor_all, :roles => :app do
    sudo "monit unmonitor all"
  end

And then these hooks:

ruby
before 'deploy', "deploy:unmonitor_all"
after 'deploy', 'deploy:monitor_all'
Avatar

It actually does, but since it becomes unstickied it moves down the list.

Avatar

Perhaps more experienced Rails users know this... After adding the Mime::Type.register_alias "text/html", :mobile I got undefined methodref' for nil:NilClass` when trying to go to the mobile site. When I restarted my local rails server, the error went away.

Avatar

How can I list specific tags in route file? I want to create a link and filter specific tags. I want to be able to link the path to specific filtered tags for my posts using link_to. e.g. <%= link_to "Blog", posts_path %>

ruby
puts "
  get 'tags/:tag', to: 'posts#index', as: :tag"
Avatar

Awesome gem! I'm a new Rails user and always thought that the error output was ugly and not at all very helpful. Thanks a bunch!

Avatar

Update: To make this work in XLS format, we need to pass in the tab character, so I modified my controller to look like this:

ruby
@products = Product.find_all_by_color("green")
respond_to do |format|
  format.csv { send_data Product.to_csv({},@products) }
  format.xls { send_data Product.to_csv({col_sep: "\t"}, @products) }
end

And then this is how product.rb receives the call of .to_csv with the tab character and @products passed in as variables from the controller:

  def self.to_csv(options,products)
    CSV.generate(options) do |csv|
      csv << column_names
      products.each do |product|
        csv << product.attributes.values_at(*column_names)
      end
    end
  end
Avatar

When I upgraded to Rails 3.2 I had issues with running the seed tasks, only in production. When the seed file ran, it would not find any ActiveRecord models; but would work fine when called from the console.

Turns out, threadsafe was enabled in production, and rake tasks would get tripped up. To fix I had to do:

app/production.rb
   # Enable threaded mode
   config.threadsafe!  unless $rails_rake_task

also a good thing for capistrano recipes.

Avatar

I am having the same error, even when I test with an Administrator account. :-(

Any idea?

Avatar

Hello,

Thanks for gem.I have success fully completed all the thing.But at last I stuck.

I am using private chat 1-1 user.
When user starts chat, it works fine.But if user close his window(like facebook).and again clicks on start button ,then he gets two time same message.and if again he close than he gets 4 time same message.finally I fond the error, subscribe_to create same channel when window is open.and it published message 2 times.I tried to handle this things a lot from my side still I am getting problem.

how can I check whether channel it is exist or not.or any other solution for that.

Please help me for that.

Thaks
Arun

Avatar

@Dennis I'd also like to know this. I'm new to Rails and looking for the best way to populate my DB with fake data. Rails seems to have come a long way, so I'd like to be sure.

Avatar

Just solved this - it's a problem between turbolinks and jQuery UI. Installed this gem, problem disappeared.

https://github.com/kossnocorp/jquery.turbolinks

Avatar

Hey Mihai - I'm having the same issue (but I'm using Bootstrap, not ZURB). Did you ever figure out what the problem was?

Avatar

I noticed this screencast is pretty old. As some have mentioned, there's a faster faker (ffaker) now.

When I search through repos on GitHub, I noticed a lot of people are using the ffaker gem in their specs/ directory.

I'm new to Rails, and I'm not familiar with the specs/ directory yet. Is the info in this screencast still a good way to populate your DB with random fake data?

Avatar

Not a rails expert, but found the solution for anyone else with the issue. Rails 4 now uses strong params with protecting attributes now done in the controller

ruby
...

  def create
    respond_with Entry.create(entry_params)
  end

  def update
    respond_with Entry.update(params[:id], entry_params)
  end

...

  def entry_params
    params.require(:entry).permit(:name, :winner)
  end

...
Avatar

i keep coming back to this screencast; the best screencast on TDD i've seen so far.

Avatar

Solve by unicorn stop and then unicorn start instead of restart.

Avatar

Great vid mate, however using Rails 4 I'm getting the following error on my create and update methods:

ActiveModel::ForbiddenAttributesError

Avatar

@chris, I am trying to limit the answer to one, did you find a way to prevent the Add Answers link from being rendered if we already have reached our limit?

Avatar

Alternatively, set the default format in the routes.rb file:

routes.rb
Raffler::Application.routes.draw do
  resources :entries, defaults: { format: :json }

  root to: 'raffle#index'
end
Avatar

did you get this to work? and how?
I am using Rails 4

Avatar

@branden, did you get an answer for this? I want to use has_one and belongs_to relationship for question-answer. and it is not working

Avatar

please can some one help me with this problem here? I am using rails 4. http://stackoverflow.com/questions/21295989/nested-attributesmodels-forms-not-working-as-needed-in-rails-4

I want to use a has_one relationship for questions-answer.

Thanks

Avatar

Solved... It was just a stupid typo.... Sorry guys..

Avatar

Update for anyone using RedCarpet 3.0.0

Using this RailsCast, just change the helper method to this:

ruby
def markdown(blogtext)
    renderOptions = {hard_wrap: true, filter_html: true}
    markdownOptions = {autolink: true, no_intra_emphasis: true}
    markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(renderOptions), markdownOptions)
    markdown.render(blogtext).html_safe
 end

Then in view just use

ruby
<%= markdown(@blog.body) %>

Notice they changed to sets of hashes for options. One hash of options for the Redcarpet::Render::HTML.new(options hash here)
and the second options hash for the Redcarpet::Markdown.new(renderer(this is where the renderer goes), markdown hash here). To understand, here are both without removing options into two variables.

ruby
def markdown(blogtext)
  markdown = Redcarpet::Markdown.new(Redcarpet::Render::HTML.new({hard_wrap: true, filter_html: true}), {autolink: true, no_intra_emphasis: true})
  markdown.render(blogtext).htm_safe
end

Check their github site to find out which options belongs in which options hash.

Avatar

Just wanted to point out that I'm working on a gem called "Headhunter" that overtakes Deadweight's functionality and also validates HTML and CSS all in one automagically in your Rails app. For this, it injects itself as middleware into the Rack stack and validates every HTML response that any feature tests triggers.

It would be great to get some feedback, check it out! https://github.com/jmuheim/headhunter

Avatar

Do you solve this problem? I have the same one.

Avatar

Hey, is it possible for pgSearch to search whole phrases? If I have a text called "On Hold" and a text called "On Hold - later", I want the search result to return only "On Hold" if the search term is "On Hold".

Is this possible? Thanks in advance!

Avatar

Actually I got that wrong... Here is the code that worked for me. Again the link I put in the initial response was where I got this code...

Products Controller
def product_params
    params.require(:product).permit(:name, :product_type_id).tap do |whitelisted|
        whitelisted[:properties] = params[:product][:properties]
end
Avatar

From my understanding you have to use tap and then whitelist the dynamic fields. Here is the code I used in my application.

ruby
def product_type_params
      params.require(:product_type).permit(:name).tap do |whitelisted|
              whitelisted[:product_fields] = params[:product_type][:product_fields]
      end
end

Here is the resource I used to figure this out.
Link

I hope that helps.

Avatar

I know this is a little late but I recently released a gem for this exactly this purpose. Do check out Cadinsor.

Avatar

Hi, are these instructions still current? Are they meant to work with Rails 4? I have devise, omniauth and have separate user and authentication models. I'm trying to follow along with this but am not getting the results being demonstrated. Is it possible that this is no longer current? Thank you, OH

Avatar

I am having the same issue, and still not clear how you resolved it, can you paste your before and after code?

Thanks!