Question

I'm struggling to bug fix this issue with my FormTagHelper where the params I'm attempting to pass through are being interpreted as methods - hence the no method error.

Here's the view:

<%= form_tag update_user_email_admin_user_path do %>
 <%= text_field :user, :email, placeholder: 'E-mail or user id' %>
 <%= text_field :user, :new_email, placeholder: 'New e-mail' %>
 <%= submit_tag "Update email", class: 'btn-1 btn-round btn-size-2' %>
<% end %>

And here's the controller action (for your reference):

def update_user_email
  user_check = User.find_by_email(params[:user][:new_email].downcase.strip)

  if user_check.blank?
    @user.email = params[:user][:new_email].downcase.strip
    @user.save
    flash[:notice] = "Email updated"
  else
    flash[:alert] = "This email is already being used by someone else!"
  end
end

And when I load the page in development, this is the no method error:

NoMethodError at /admin/users/195455 undefined method 'new_email' for #<User:0x007f9198c3f560>

This confuses me because I've used several other FormTagHelpers in the app, with almost identical syntax, and they work fine.

Would love an extra set of eyes on this. I'm a n00b so many thanks in advance!!

Was it helpful?

Solution

Because Its assuming you are trying to save value of a User in new_email field, that doesn't exist in your users table. Please use text_field_tag for this kind of form. you can find the docs here:

<%= form_tag update_user_email_admin_user_path do %>
 <%= text_field_tag 'email', nil , placeholder: 'E-mail or user id' %>
 <%= text_field_tag 'new_email', nil , placeholder: 'New e-mail' %>
 <%= submit_tag "Update email", class: 'btn-1 btn-round btn-size-2' %>
<% end %>

Hope it will help.Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top