user2911232
user2911232

Reputation:

Retrieve parameters from a custom form

I am working on creating a contact form for users to contact admins.

On users_controller.rb I created the method "ticketsupport":

  def ticketsupport
    subjectF = params[:user][:subjectsupport]
    messageF = params[:user][:messagesupport]
    if UserMailer.support_ticket(current_user.username, current_user.email, subjectF, messageF).deliver 
      flash.alert = "Your ticket has been recorderd. We will come back to you shortly."
      redirect_to user_path(current_user)
    end
  end

On application.html.erb I created a link that unhides the bootstrap's modal:

<a href="#supportModal" data-toggle="modal">Need Help? Submit a Support Ticket</a>

  <!-- Support Modal -->
  <div id="supportModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="supportModalLabel" aria-hidden="true">
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="supportModalLabel">Contact Support</h3>
  </div>
  <div class="modal-body">
    <%= render "supportform" %>
  </div>
  <div class="modal-footer">
    <button class="btn btn-primary">Send ticket</button>
  </div>
</div>

You can find the form I render here. And lastly, in routes added this: get "supportform" => "users#ticketsupport"

The error I get when I load the page is this: undefined method 'subjectsupport' for #<User:0x007ff5de93ed98> and it is generated when tries to load the form and its attributes.

I just want to create my custom form and pass the attributes so I can use them in the mailer.

Any idea what I am doing wrong?

Upvotes: 1

Views: 53

Answers (2)

Paulo Henrique
Paulo Henrique

Reputation: 1025

The method subjectsupport is not defined on the user model. To use custom attributes you should be using form_tag.

form_for and simple_form_for looks for an attribute for each input you create and since your models does not respond to it, it raises an error. If you want to use this attributes, use form_tag instead.

Create attributes on your model only because of the need of a view its not a good idea, you can use form_tag or take a more advanced soluation which is create a custom model that responds to your custom attributes and to the User model attributes.

Upvotes: 1

Ahmad Sherif
Ahmad Sherif

Reputation: 6213

You can add an attribute accessor in your User model for subjectsupport, like this

class User < ActiveRecord::Base
  # ...
  attr_accessor :subjectsupport
  attr_accessible :subjectsupport # though you better use strong parameters
  # ...
end

This should do the trick.

Upvotes: 1

Related Questions