mxabierto / consulta

Fork :fork_and_knife: mexicano de Cónsul
GNU Affero General Public License v3.0
0 stars 0 forks source link

responsible_name no me deja guardar o actualizar objetos #38

Closed defvol closed 9 years ago

defvol commented 9 years ago

Probando en rails console

=> #<Proposal id: 33, title: "La Ley de Herodes", ..., responsible_name: "Ron Swanson"

irb(main):004:0> p.valid? ActsAsTaggableOn::Tag Load (1.6ms) SELECT "tags".* FROM "tags" INNER JOIN "taggings" ON "tags"."id" = "taggings"."tag_id" WHERE "taggings"."taggable_id" = $1 AND "taggings"."taggable_type" = $2 AND (taggings.context = 'tags' AND taggings.tagger_id IS NULL) [["taggable_id", 33], ["taggable_type", "Proposal"]] User Load (1.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 58]]

=> false

irb(main):005:0> p.errors

=> #<ActiveModel::Errors:0x007fb528bc6300 @base=#<Proposal id: 33, title: "La Ley de Herodes", ... responsible_name: nil, summary: "La ley de Herodes es una película mexicana de 1999...", video_url: "", physical_votes: 0, category: 1, closing_date: nil>, @messages={:responsible_name=>["no puede estar en blanco", "es demasiado corto (6 caracteres mínimo)"]}> irb(main):006:0>

defvol commented 9 years ago

El seed task también falló:

Reiciendis ut in dolorem labore voluptas.
Eligendi molestias libero.
Esse minima tempore labore iure perspiciatis.
Doloremque velit et dolorem.

Creating Proposals rake aborted! ActiveRecord::RecordInvalid: La validación falló: Responsible name no puede esta r en blanco, Responsible name es demasiado corto (6 caracteres mínimo) /Users/rodolfo/c/consulta/db/dev_seeds.rb:88:in block in <top (required)>' /Users/rodolfo/c/consulta/db/dev_seeds.rb:85:in<top (required)>'

defvol commented 9 years ago

Reinicié

bundle exec rake db:drop
bundle exec rake db:setup

Hice una org y mi cuenta parece validada

screen shot 2015-11-18 at 1 00 17 am screen shot 2015-11-18 at 1 00 05 am

y aún así:

screen shot 2015-11-18 at 1 00 26 am

defvol commented 9 years ago

El admin tampoco pudo

screen shot 2015-11-18 at 1 09 40 am

defvol commented 9 years ago

En la vista de crear propuestas, no aparecen los campos para llenar responsible_name porque al parecer el que crea la propuesta ya está verificado:

https://github.com/mxabierto/consulta/blob/mvp/app/views/proposals/_form.html.erb#L53

defvol commented 9 years ago

A pesar de quitar esa verificación para que el campo apareciera en NEW, la forma arroja error de que el campo no puede estar en blanco

screen shot 2015-11-18 at 12 19 36 pm

defvol commented 9 years ago

con byebug

[27, 36] in /Users/rodolfo/c/consulta/app/controllers/concerns/commentable_actions.rb
   27:     load_featured_tags
   28:   end
   29: 
   30:   def create
   31:     byebug
=> 32:     @resource = resource_model.new(strong_params)
   33:     @resource.author = current_user
   34: 
   35:     if @resource.save_with_captcha
   36:       track_event
(byebug) resource
#<Proposal id: nil, title: "La propuesta del admin", description: "<p>BAR</p>\r\n", question: "¿Crees en el admin?", external_url: "", author_id: nil, hidden_at: nil, flags_count: 0, ignored_flag_at: nil, cached_votes_up: 0, comments_count: 0, confirmed_hide_at: nil, hot_score: 0, confidence_score: 0, created_at: nil, updated_at: nil, responsible_name: "Administrador del Sistema", summary: "FOO", video_url: "", physical_votes: 0, category: 0, closing_date: nil>
(byebug) 
(byebug) resource.responsible_name
"Administrador del Sistema"
(byebug) 
(byebug) resource.valid?
false
(byebug) resource.errors
#<ActiveModel::Errors:0x007f8482afc5e0 @base=#<Proposal id: nil, title: "La propuesta del admin", description: "<p>BAR</p>\r\n", question: "¿Crees en el admin?", external_url: "", author_id: nil, hidden_at: nil, flags_count: 0, ignored_flag_at: nil, cached_votes_up: 0, comments_count: 0, confirmed_hide_at: nil, hot_score: 0, confidence_score: 0, created_at: nil, updated_at: nil, responsible_name: "Administrador del Sistema", summary: "FOO", video_url: "", physical_votes: 0, category: 0, closing_date: nil>, @messages={:author=>["no puede estar en blanco"]}>
(byebug) 

(byebug) resource.author
nil
(byebug) resource.author = current_user
(byebug) resource.valid?
false
(byebug) resource.errors
#<ActiveModel::Errors:0x007f8482afc5e0 @base=#<Proposal id: nil, title: "La propuesta del admin", description: "<p>BAR</p>\r\n", question: "¿Crees en el admin?", external_url: "", author_id: 1, hidden_at: nil, flags_count: 0, ignored_flag_at: nil, cached_votes_up: 0, comments_count: 0, confirmed_hide_at: nil, hot_score: 0, confidence_score: 0, created_at: nil, updated_at: nil, responsible_name: nil, summary: "FOO", video_url: "", physical_votes: 0, category: 0, closing_date: nil>, @messages={:responsible_name=>["no puede estar en blanco", "es demasiado corto (6 caracteres mínimo)"]}>
(byebug) resource.responsible_name
nil

WTF

defvol commented 9 years ago

responsible se modifica before_validation

  protected

    def set_responsible_name
      if author && author.level_two_or_three_verified?
        self.responsible_name = author.document_number
      end
    end

Sin embargo nuestros valores están bien:

(byebug) current_user.nil?
false
(byebug) current_user.level_two_or_three_verified?
true
defvol commented 9 years ago

Cuando pregunto valid? se va a set_responsible_name:

[27, 36] in /Users/rodolfo/c/consulta/app/controllers/concerns/commentable_actions.rb
   27:     load_featured_tags
   28:   end
   29: 
   30:   def create
   31:     byebug
=> 32:     @resource = resource_model.new(strong_params)
   33:     @resource.author = current_user
   34: 
   35:     if @resource.save_with_captcha
   36:       track_event
(byebug) resource.valid?

[121, 130] in /Users/rodolfo/c/consulta/app/models/proposal.rb
   121: 
   122:   protected
   123: 
   124:     def set_responsible_name
   125:       byebug
=> 126:       if author && author.level_two_or_three_verified?
   127:         self.responsible_name = author.document_number
   128:       end
   129:     end
   130: 
(byebug) 
defvol commented 9 years ago

Es importante que byebug esté después de @resource.author = current_user, para que no se haga nil el responsible_name

defvol commented 9 years ago

se comporta raro, a pesar de que author existe y está verificado, de repente responsible_name se vuelve nil

la solución propuesta por @babasbot es ignorar esa verificación

    def set_responsible_name
      self.responsible_name = author.name
    end
defvol commented 9 years ago

Con el fix:

   30:   def create
   31:     @resource = resource_model.new(strong_params)
   32:     @resource.author = current_user
   33:     byebug
   34: 
=> 35:     if @resource.save_with_captcha
   36:       track_event
   37:       redirect_path = url_for(controller: controller_name, action: :show, id: @resource.id)
   38:       redirect_to redirect_path, notice: t('flash.actions.create.notice', resource_name: "#{resource_name.capitalize}")
   39:     else
(byebug) @resource.responsible_name
"Winston Smith"
(byebug) @resource.author.name
"Winston Smith"
(byebug) @resource.valid?
true