samedi 27 juin 2015

How to display error messages in a multi-model form with transaction?

Two models, Organization and User, have a 1:many relationship. I have a combined signup form where an organization plus a user for that organization get signed up.

The problem I'm experiencing is: When submitting invalid information for the user, it renders the form again, as it should, but the error messages (such as "username can't be blank") are not displayed. The form does work when valid information is submitted and it does display error messages for organization, just not for user.

How should I adjust the code below so that also the error messages for user get displayed?

def new
  @organization = Organization.new
  @user = @organization.users.build
end

def create
  @organization = Organization.new(new_params.except(:users_attributes))
  #Validations require the organization to be saved before user, as user requires an organization_id. That's why users_attributs are above excluded and why below it's managed in a transaction that rollbacks if either organization or user is invalid. This works as desired.

  @organization.transaction do
    if @organization.valid?
        @organization.save
        begin
          @organization.users.create!(users_attributes)
        rescue
          # Should I perhaps add some line here that adds the users errors to the memory?
          raise ActiveRecord::Rollback
        end
     end
  end

  if @organization.persisted?
    flash[:success] = "Yeah!"
    redirect_to root_url
  else
    @user = @organization.users.build(users_attributes) # Otherwise the filled in information for user is gone (fields for user are then empty)
    render :new
  end

end

The form view includes:

<%= form_for @organization, url: next_url do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
    <%= f.text_field :name %>
        # Other fields

    <%= f.fields_for :users do |p| %>
        <%= p.email_field :email %>
            # Other fields
    <% end %>

    <%= f.submit "Submit" %>
<% end %>

The error messages partial is as follows:

<% object.errors.full_messages.each do |msg| %>
  <li><%= msg.html_safe %></li>
<% end %>

Heroku not updating website after successful deploy

I'm trying to deploy my app through Heroku, using Ruby on Rails, and I have been successful in the past but now when I use the same commands as before it will deploy successfully but when I visit my website it has not been updated with my new code.

Here are the commands I am using:

git add -A
git commit -m "message"
git push
git push heroku
heroku run rake db:migrate

I tried using "git push heroku master" as well based on another stack overflow thread but it didn't update my website either. Running the deployment only returns one warning and that is that my Ruby version is not declared so I don't think that would be an issue. I'd be happy to provide more information if I need to. Any information/help would be greatly appreciated.

How to load asset gems only for precompile on heroku?

Rails 4.1+, so there isn't built-in support for an :assets group

I want to keep Heroku's precompile on push behaviour, but don't want the asset gems loaded by the rails server. We don't use any kind of inline coffeescript or scss template rendering in the app, only in the assets, so it's just wasted memory at runtime.

I've played around with extending the rake task, configuring sprocket-rails, and even changing application.js to application.js.erb and adding things like

//= <% require 'jquery-rails' %> 
//= require 'jquery'

but still get these errors:

Sprockets::FileNotFound: couldn't find file 'jquery'

If I keep the asset gems in the default Gemfile group everything works fine.

The point here is to not have them loaded in the production environment, but to have

RAILS_ENV=production rake assets:precompile task 

load them before it executes (and fails because of missing libraries)

wrong singular for 'slaves'

I have a line resources :slaves in my routes.rb. Which is definitely plural for 'slave', but rails thinks that it's plural form of 'slafe', so I get paths like new_slafe_path. Is there a way to tell rails correct singular form without explicitly specifying each route?

Undefined method "errors"... But there are ERRORS

I am doing a basic validation for a bank entry and am trying to display the errors on submit. Here is my validation and html.

class GuestbookEntry < ActiveRecord::Base
  validates :body, presence: :true
end

This is my html:

<% form_for @guestbook_entry do |f| %>                                                                                   
  <% if @guestbook_entry.errors.any? %>                                                                                
    <% @guestbook_entry.errors.full_messages.each do |m| %>                                                                                         
      <li><%= m %></li>                                                                                          
    <% end %>                                                                                                   
  <% end %>                                                                                 
  <%= f.label :body, "Guestbook Entry:" %>                                                                                     
  <%= f.text_area :body %>                                                                                                         
  <%= f.submit "Submit" %>                                                                                     
<% end %>

error message Any ideas?

Rails 4.1 - Write to MySQL database without typecasting

I have a column in my MySQL database which is of type TINYINT(1). I need to store actual integers in this column. The problem is, because of the column type, Rails 4.1 assumes this column contains only boolean values, so it typecasts all values besides 0 or 1 to be 0 when it writes to the database.

I don't want to simply disable boolean emulation since we have a number of columns in our database where we use TINYINT(1) to actually represent a boolean value.

How can I force Rails 4.1 to bypass the typecasting step and write directly to the database instead?


(This excerpt from the Rails 4.1 source may be of some use: http://ift.tt/1BWF1og)

samedi 9 mai 2015

Same commands, different results

I saw a weird behavior in my bash terminal. I run same commands on my terminal command line, but get different results. Following lines are copied-pasted from my bash terminal windows:

me@me:~$ ls "/media/me/My Passport/Archive U/‌"
ls: cannot access /media/me/My Passport/Archive U/‌: No such file or directory
me@me:~$ ls "/media/me/My Passport/Archive U/‌"
Backup  Documents  Downloads  Music  Pictures  Videos

I tried both commands using up arrow key (to bring previous commands to the current line) many times and the result does not changes. To be sure that the command has no hidden character, I selected both commands and pasted it to an spreed sheet and converted each character to its code and compared. there was no difference. I tried to select each commands and right-click, choose Copy, right click again, choose paste. If I copy-paste first command, I get first result. I f I copy-paste second command, I get second result!!!

Could any one say what is the difference between two commands? I guess no one could!

I tell you what is the difference. First command is copied and pasted from a text file!

I am using Ubuntu 14.04.