Note that there are some explanatory texts on larger screens.

plurals
  1. PORails model not getting the params correctly
    primarykey
    data
    text
    <p>i'm having a kinda weird &amp; annoying bug with Rails. I have a HAML form, a model, and a method for handling it:</p> <p>View</p> <pre><code>%form{ :action =&gt; "/new", :method =&gt; "post", :style=&gt;"margin-top: 6px"} %input{:type=&gt;"hidden", :name=&gt;"authenticity_token", :value=&gt;form_authenticity_token.to_s} %input{:type =&gt; "text", :name =&gt; "blogName", :placeholder=&gt;"Blog name"} %input{:type =&gt; "text", :name =&gt; "blogSubdomain", :placeholder=&gt;"Blog URL"} %input{:type =&gt; "text", :name =&gt; "username", :placeholder=&gt;"username"} %input{:type =&gt; "text", :name =&gt; "email", :placeholder=&gt;"email"} %input{:type =&gt; "password", :name =&gt; "password", :placeholder=&gt;"password"} %br/ %input{:type =&gt; "submit", :value =&gt; "Send", :class =&gt; "btn btn-primary"} - unless session[:error].nil? %div{:class=&gt;"alert alert-error", :style=&gt;"font-size:13px; font-weight:normal;"} %strong Please correct the following errors %br - session[:error].each do |value| - unless value.nil? %li= "#{value}" - session[:error]=nil </code></pre> <p>Model:</p> <pre><code>class User include MongoMapper::Document key :screen_name, String, :required =&gt; true, :unique =&gt; true key :blog_name, String, :required =&gt; true, :unique =&gt; true key :blog_subdomain, String, :required =&gt; true, :unique =&gt; true key :email, String, :required =&gt; true, :unique =&gt; true, :format =&gt; /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i key :password, String, :required =&gt; true key :admin, Boolean timestamps! validate :finalValidate before_save :stripSpaces, :hashPassword def stripSpaces self.blog_subdomain = self.blog_subdomain.gsub(/[\ _]/, "-") end def finalValidate if blog_subdomain.length &gt; 10 errors.add(:blog_subdomain, "Your chosen subdomain is too long, the maximum is 9 characters") end case blog_subdomain when "www" when "api" errors.add(:blog_subdomain," - Sorry but that subdomain is reserved!") end end def hashPassword self.password = Digest::SHA512.hexdigest(self.password) end end </code></pre> <p>And the method to do it</p> <pre><code>def new_post if session[:r] redirect_to root_path, :subdomain =&gt; nil else user = User.new({ :screen_name=&gt;params[:username], :blog_name =&gt; params[:blogName], :blog_subdomain =&gt; params[:blogSubdomain], :email =&gt; params[:email], :password =&gt; params[:password], :admin =&gt; false }) if user.save session[:screen_name] = user.screen_name session[:blog_subdomain] = user.blog_subdomain session[:blog_name] = user.blog_name session[:twitter_user] = "nothin" session[:r] = true flash[:success] = "Blog created!" redirect_to root_path, :subdomain =&gt; user.blog_subdomain else errors = Array.new() for i in 0..user.errors.full_messages.count errors.push(user.errors.full_messages[i]) end session[:error] = errors flash[:error] = "Error creating blog" redirect_to '/new' end end end </code></pre> <p>The method fails on <code>if user.save</code>, going straight to the <code>else</code> statement. I'm getting errors when passing the <code>email</code> and <code>password</code>. MongoMapper returns:</p> <ul> <li>Email can't be blank</li> <li>Email has already been taken</li> <li>Email is invalid</li> <li>Password can't be blank</li> </ul> <p>If I remove the validations then the values are just <code>nil</code>. I double checked all, but I couldn't get what's wrong. On the log I see how the params are being sent:</p> <pre><code>Started POST "/new" for 127.0.0.1 at 2012-03-10 20:46:56 +0100 Processing by ActionsController#new_post as HTML Parameters: {"authenticity_token"=&gt;"T7J8DDaWWd25LBP6dRgbvpAs4bkC/zLk3GiQ5rVLmiw=", "blogName"=&gt;"wut", "blogSubdomain"=&gt;"WUT", "username"=&gt;"someuser", "email"=&gt;"some@validmail.net", "password"=&gt;"[FILTERED]"} Redirected to http://myapp.dev/new Completed 302 Found in 745ms </code></pre> <p>What I'm doing wrong?</p> <h3>EDIT: Put a logger on the model, and bot <code>mail</code> and <code>password</code> classes are <code>NilClass</code></h3>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload