RailsCasts Pro episodes are now free!

Learn more or hide this

Recent Comments

Avatar

Please check my gem https://github.com/igorkasyanchuk/any_login
It can really make your life easier with changing logged in user.
Your feedback is appreciated.
Video: https://www.youtube.com/watch?v=978DlHvufSY&feature=youtu.be
PS: this is my first solid gem :) and I hope you will like it.

Avatar

Note, there is now a gem to do some of this in helpers...

https://github.com/rsantamaria/papercrop

Avatar

Thanks you so much,saved a lot of time for a feature I had to add.
Wondering if there is a way for generating the same .csv file so it would be saved in the rails public direcoty instead.
I have a script I wrote for that csv file
help will be much appriciated,thanks

Avatar

How to pass locales for question_fields partial. Can anyone help me out.

<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder %>
<% end %>

I have tried the following way and tried to access type in question_fields. It's thrown error(Undefined variable type)

<%= f.fields_for :questions do |builder| %>
<%= render "question_fields", :f => builder, :type => "Test" %>
<% end %>

Avatar

If you´re using this with choosen-select, remember to add the trigger method:

if options
$('#person_state_id').html(options)
$('#person_state_id').parent().show()
$("#person_state_id").trigger("chosen:updated");

Avatar

Had a problem which took me a while to figure out.

Faye channels were not being subscribed due to GET parameters in the channel name EX: "/conversation/3?locale=pt".
Faye would then send a response with error 405 invalid channel.
In case anyone has the same problem simply add

ruby
  your_channel_name.split('?').first
Avatar

The deploy.rb is the configuration that drives the cap command. Recipes that aren't included in the configuration won't be available to the cap command. Use this site for more writing information. http://www.essayscouncil.com

Avatar

This is the correct syntax for scoping in rails 4+

scope :with_role, -> role { where("roles_mask & #{2**ROLES.index(role.to_s)} > 0 ") }
Avatar

I have been trying to model a similar domain, but the price has to be on the join table, since the price is not known until purchased (think auction). I have read every post on the internet pertaining to has_many through with additional attributes on the join table and I am still not getting it.

I forked the repo https://github.com/jamlevi/017-habtm-checkboxes-revised and added a new folder "store-move-price-to-join". I added views/categories & controllers/categories_controller.rb. Using rails 3.2.17.

I realize this page/thread are pretty old, but I am hoping somebody out there sees this and can help me out (plus the same question is unanswered from 2 years ago). Pretty sure I need a fields_for in views/categories/_form.html & accepts_nested_attributes_for in category. A working repo would be tremendously helpful, instead of code snippets buried in many similar-but-not-same use case posts scattered throughout the intenet.

Avatar

I actually got this to work just now with Sorcery.

This is what the code has to be to work:

def current_account
current_user.account
end

helper_method :current_account

def scope_current_account
Account.current_id = current_account
yield
ensure
Account.current_id = nil
end

I believe adding the ability to change the scope for the user model was so an account admin could add new users with the correct account id. Currently this is the part that threw me with Sorcery since it doesn't allow this (at least not using this method). I could make it so when the admins create a new user, the account_id gets added via something like @user.account_id = @account.id.

Avatar

Anyone get this working with sorcery?

This isn't working for me without subdomains. It seems current_account remains on the first account created no matter what. So basically when account2 logs in, it can see all the records for account 1 and when account2 creates a record, the record gets assigned the account_id of account1.

Avatar

There's a branch that works for Rails 4.2, specify this on your Gemfile
gem 'client_side_validations', github: "DavyJonesLocker/client_side_validations", branch: "4-2-stable"

Be sure to check the Readme on the Github page, some things have changed a bit.

Avatar

with the latest versions , the event.preventdefault() does not seem to make any difference,any ideas?

Avatar

Creston, thanks, this is always the pain with development, outdated videos.

I found that this :Rake::Task[:production].invoke: needed to be added to the end of the Cap File

Avatar

I need to tell how create this UserController is just :
rails g scaffold user name:string email:string or have any way ?

Avatar

I was having trouble to setup postgresql on my environment, the thing is that I'm working with https://c9.io, and just for the record, Cloud9 has his own setting up instructions https://docs.c9.io/docs/setting-up-postgresql.

Avatar

Thanks a bunch bruv! Was pissing about with paperclip last week with no joy.m Subscribed this morning and got it working!

Avatar

can some one help about tracking when the user logged in all other user will get message of their logged in.
thanks

Avatar

Will these two interfere if m is the same instance but it is used in different partials?

#in _detailed.html.erb
<% cache m do %>
 ... Detailed view...
<% end %>

and then

#in _row.html.erb
<% cache m do %>
 ... row in the list...
<% end %>
Avatar

Wonderful! Just what I was looking for! :)

Avatar

I was really struggling with CSV import and this thread has really helped me out... thanks a lot!

Avatar

Hi I am new to Ruby on rails, I have followed this cast but i am running into a big issue now,

<%= mp.fields_for :projectmembers do |pm| %>
<%= render 'projectmember_fields', pm_form: pm %>
<% end %>
<tr><td class="addfld"><%= link_to_add "Add", mp, :projectmembers %></td></tr>

Here this render doesnt sends the local variable to the partial i am calling. but when i remove this link_to_add method it gets the local variable. Could you please help me on this

this is the query i have posted

link

Avatar

Also, you need to bind to the parent. Mine (coffee script):

$('#products').on 'click', '.pagination a',  ->
Avatar

I have upgraded this episode to use Ruby 2.2.2, Rails 4.2.3 and ActiveMerchant 1.52.0. Here is the source code : Paypal Express

Avatar

You can see that you are basically writing a primitive version of Rails.
Nice to see how the internals work!

Avatar

I was struggling with that issue... almost smacked my head against the walls. Really don't know why it took me too much to come and read the comments.

Thank you very much!

Avatar

Hello Friends i know i m new here but i m sharing an html email Composer with mysql database php code here and i hope this may be helpful for anyone

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "">http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

Compose HTML Email

<?php

$id=secureget('id');

$uinfo = mysql_query("SELECT * FROM mydata WHERE id=$id");
$guinfo = mysql_fetch_array($uinfo);
$uname = $guinfo['user'];
$email = $guinfo['email'];

if (isset($_REQUEST['subject'])&&(isset($_REQUEST['msg'])))
//if "email" is filled out, send email
{
//send email
$subject = $_REQUEST['subject'];
$team = $_REQUEST['team'];
$name = $_REQUEST['name'];
$foot = 'Best regards,Company '.$team.' '.$name.'
Support Email - email@email.com

Copyrights [c] 2015 Company
';
$to = ''.$email.'';
$mail = 'Company '.$team.'c@mail.com';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers

$headers .= 'From: '.$mail."\r\n".
'Reply-To: '.$mail."\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";

$message1 = $_REQUEST['msg'];
$message .= ''.$message1.'
'.$foot.'';

$do = mail($to, $subject, $message, $headers);
if($do){
echo "Sent...";
}
else {
echo 'Failed!';
}
}

Read full Code and article here http://quickwebinfo.com/blog/2015/08/how-to-compose-a-html-email-using-mysql-database-php

Avatar

Hello Friends i know i m new here but i m sharing an html email Composer with mysql database php code here and i hope this may be helpful for anyone

<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "">http://www.wapforum.org/DTD/xhtml-mobile10.dtd">

Compose HTML Email

<?php

$id=secureget('id');

$uinfo = mysql_query("SELECT * FROM mydata WHERE id=$id");
$guinfo = mysql_fetch_array($uinfo);
$uname = $guinfo['user'];
$email = $guinfo['email'];

if (isset($_REQUEST['subject'])&&(isset($_REQUEST['msg'])))
//if "email" is filled out, send email
{
//send email
$subject = $_REQUEST['subject'];
$team = $_REQUEST['team'];
$name = $_REQUEST['name'];
$foot = 'Best regards,Company '.$team.' '.$name.'
Support Email - email@email.com

Copyrights [c] 2015 Company
';
$to = ''.$email.'';
$mail = 'Company '.$team.'c@mail.com';

// To send HTML mail, the Content-type header must be set
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

// Create email headers

$headers .= 'From: '.$mail."\r\n".
'Reply-To: '.$mail."\r\n" .
'X-Mailer: PHP/' . phpversion();
$headers .= 'BCC: ' . implode(', ', $recipients) . "\r\n";

$message1 = $_REQUEST['msg'];
$message .= ''.$message1.'
'.$foot.'';

$do = mail($to, $subject, $message, $headers);
if($do){
echo "Sent...";
}
else {
echo 'Failed!';
}
}

Read full Code and article here http://quickwebinfo.com/blog/2015/08/how-to-compose-a-html-email-using-mysql-database-php

Avatar

Hi
I am Paul
I am interested in Monogid.
But I have no any experience about Mongo.
And I am going to run application with MongoID on my local.
But I don't know how to run it on my local.
Who can help me to run a rails application with mongoid?
Please help me.

Avatar

Update:
Capistrano 3 has moved out bundler integration into a gem, so
require 'bundler/capistrano' won't work anymore, you need to have 'capistrano/bundler' in your Gemfile and require it and everything will be fine. (See http://stackoverflow.com/a/19412601/1216245.)

Avatar

I have an odd case where my code created with the help of this rails casts for HABTM checkboxes was working fine... an then all of a sudden I'm getting a NoMethodError: undefined method `name' for nil:NilClass error when creating or updating a record that has project_ids filled in.

If I try to use the method in the rails console I get the error specifically at the point of adding project_id's like this:

p = Project.last

works and returns project field data

p.person_ids = [1,2]

  1. runs 3 SQL statements, selecting the person, selecting through a join my assignment of the person to the project, and then inserting the assignment
  2. then it says rollback transaction
  3. and displays the method NoMethodError: undefined method `name' for nil:NilClas

And the weird part is, that once that all happens, if I call p.save after the errors list, it actually saves properly adding the two people assignments to the project

I've rolled back the git history to see if I did something else, but even if I go way back to previous versions that worked for sure it still gives the same error. This makes me think it's database related??

Avatar

Your "edit" form object can often be a subclass of your "new"/"create" form object, that simply sets certain fields as read-only. Virtus, for one, makes that easy (where they're called private attributes).

Avatar

Thank you! Was scratching my head there, but now all is well. Sometimes it's the simplest things …

Avatar

id is increasing when making guest users . what is the best way to solve this problem... please help

Avatar

Overriding model in api controller was an excellent piece of code. But can you guide me how and where to put this class in a separate file and include it in api controller.

Avatar

Thank you Ryan! After years this worked like a charm.

@VISHAL DEEPAK

A couple things I would suggest for you:

  • For redirecting to Paypal, my application's user flow is similar to yours; however, I would suggest using a form to send a POST request to the Paypal url instead of get as required by Paypal. You can use jquery to dynamically inject the form and submit and then remove it like so:
js.erb
$('<form action="<%= encrypted_paypal_url %>" method="post" target="_blank"></form>').appendTo('body').submit().remove();
  • You have an error because you're trying to parse a string to query (see my solution below)
  • For your url you only returned encrypted values and not the additional fields indicating to Paypal that this is encrypted. Following my approach above, you can return the url like so (append this to the end of paypal_url):
ruby
encrypted = {
      :cmd => "_s-xclick",
      :encrypted => encrypt_for_paypal(values)
    }
    "https://www.sandbox.paypal.com/cgi-bin/webscr?" + encrypted.to_query
Avatar

Thank you Ryan! After years this worked like a charm.

@VISHAL DEEPAK

A couple things I would suggest for you:

  • For redirecting to Paypal, my application's user flow is similar to yours; however, I would suggest using a form to send a POST request to the Paypal url instead of GET as required by Paypal. You can use jquery to dynamically inject the form and submit and then remove it like so:
js.erb
$('<form action="<%= encrypted_paypal_url %>" method="post" target="_blank"></form>').appendTo('body').submit().remove();
  • You have an error because you're trying to parse a string to query (see my solution below)
  • For your url you only returned encrypted values and not the additional fields indicating to Paypal that this is encrypted. Following my approach above, you can return the url like so (append this to the end of paypal_url):
ruby
encrypted = {
      :cmd => "_s-xclick",
      :encrypted => encrypt_for_paypal(values)
    }
    "https://www.sandbox.paypal.com/cgi-bin/webscr?" + encrypted.to_query
Avatar

UPDATE:
for evaluation I used:

code
has_many :rs_evaluations, class_name: "ReputationSystem::Evaluation", as: :source

so change is just class_name

Avatar

Thanks for this video. You've saved me loads of time over the years. Hope all is well.