0
votes

First of all, I've been learning Rails for 8 months but feel like I started yesterday, so please bear with me. I'm trying to resolve why my value, called visible, isn't functioning correctly. I'm using Simple_form. 'visible' is the radio button value:

 <div class='review-form'>
      <%= simple_form_for(@review) do |f| %>
  # input boxes for current_user to put text here

  #'public' radio button, checked by default, class is for css
  <%= f.radio_button :visible, "true" , :class => "share_button" %>

  #'private' radio button, class is for css
  <%= f.radio_button :visible, "false", :class => "keep_private_button" %>

  #user can cancel
  <%= link_to I18n.t('write_review.cancel_button'), landing_page, 
  :class => 'btn' %>

  #user can submit
  <%= f.button :submit, I18n.t('write_review.submit_button'), 
  :class => 'btn btn-primary' %>

The idea is that the current_user writes a review - if they make it 'public', any other user can see it, if private, only they can see it. The radio buttons work ok - when I log out, look at the app on different machines etc, the radio button is in the last saved state. The problem is that the current review, @review, is always visible to everybody, regardless of the state of the radio button.

I put <%= @review.visible? %> on the review form. Every time I refresh the page it is 'true', even though the radio button might be false. I save the review and @review.visible then matches accordingly. But then I refresh and it goes back to true.

I think the problem could be:

In my schema.rb I have:

t.boolean  "visible",                                    :default => true

I deleted :default => trueand there was no improvement but I hear you're not supposed to manually interfere with the schema.rb anyway.

Do you think that default => true is causing the problem? Would doing a 'migration' be the way to go? How would I word that 'default => true' part in the migration?

Finally, while my schema.rb has 'boolean' my actual database has visible: tinyint(1) , which I thought was fishy, but I've read online they're in fact the same thing, so I don't think that's the problem - if you think differently, let me know.

2

2 Answers

2
votes

You shouldn't touch db/schema.rb. That is automatically updated via the migrations and/or the state of your database. You have the right idea in that you need to remove the default value for 'visible', but you need to do it in a migration. change_column_default is probably what you want.

http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_column_default

Also, the boolean vs tinyint(1) is okay. It's a MySQL thing as MySQL doesn't have true boolean types.

1
votes

To echo what Phillip said if you need to change your database, you shouldn't do it via schema.rb, you need a migration. You should have a default for booleans on mysql (because otherwise it's a tri-state instead of a binary (true, false, null).

If you think the problem is the default is wrong, you can do a change_column migration, you can generate a migration from the command line tools:

rails g migration UpdateTableName

Open the migration file (in models/db/migrate/timestamp_update_table_name.rb) and change the code to be:

def change 
    update_column :table_name, :columnname, :boolean, :default => false
end

then run this via command line tools:

rake db:migrate

If you think your problem is the form, you could always look at something like this to see if it's the values for your form that are the problem.