Rails: Devise: User - ruby-on-rails

Environment: Rails 3.2.3 with Devise
I would like to add a registration code to the registration form. The registration code is NOT in the users table. There's a separate registration_codes for that. The reason it's in the registration form, is that I would like to compare the code entered by the user filling out the registration form to what's in the registration codes table, and produce a value that will go into the users table.
I am getting an error message:
undefined method `registration_code' for #<User:0x007f91b57b7890>
and it's pointing to the code that displays the registration_code text box in views/registrations/new.html.erb
How do I get around the fact that registration_code is not part of the users table? The form was working fine, until I added the registration_code field to the form.

I would create a method in your User model that handles your registration code logic:
def registration_code=(code)
# Check registation_codes table and produce a value that will go into the users table
end
Submitting a form with this new field should now call this method with the submitted value

I assume you're using a form builder to create the form (a form_for block). In this case, you're probably using something like <%= f.text_field :registration_code %> to display the text field, but Rails can't find registration_code on the User model, because, like you said, it's not there.
Instead, try using <%= text_field_tag :registration_code %>. You can access this from your controller through params[:registration_code]

Related

Ruby on Rails: Model's attribute returns a null value when used in Views

In my seeds.rb file, I assigned a value to the name attribute of my devise model.
Admin.create!({email: 'admin#example.com', password: '12341234', password_confirmation: '12341234', name:'Steven'})
I want the value of name to show in the page when I run the application.
<h1>Welcome, <%= current_admin.name %>!</h1>
But it does not show the assigned value for name.
If you'd like the name to appear for just that one hardcoded admin, try changing your view to be like this:
<h1>Welcome, <%= Admin.first.name %>!</h1>
If you'd like the name to be dynamic, you'll need to log in as that admin (and probably want to make sure that page is accessible by only logged in admins).

rails g scaffold needs explanation - form_for submits to index?

I am trying to understand the stuff going on during the creation of an object using the pages generated by the command rails g scaffold ModelName.
What I don't understand is that given a model Location, the _form.html.erb form-tag looks like this:
<%= form_for(#location) do |f| %>
This would, if I understand it correctly, point the form submission to location_path, which is like the index (or list) of all locations stored in the database.
Why is the form not pointing to create or update? Where on the way are the objects actually created? I'd be really grateful if someone could describe the flow here, like
_form.html.erb submits to
create in locations_controller.rb which redirects to
index in locations_controller.rb, which renders
sometemplate.html.erb
Where the form submits depends on #location.new_record?.
If it’s a new record, it will POST to locations_path: /locations. That maps to LocationsController#create.
If it’s an existing record, it will PUT (or PATCH on newer versions of Rails) to location_path(#location): /locations/:id. That maps to LocationsController#update.
As #Pavan suggests in the comment, a look at the existing routes can help with understanding routing:
rake routes

Rails form redirecting to 'new' action

What I am trying to do is build a form in which the user fills some of the fields for a new Publication, and takes you to the New Publication action, with those fields already filled in, so the user fills the rest.
I got the controller part covered, but I cant find how to use form_for for this, as its not exactly associated to the model (only some of the necessary fields are in the first form).
you could do
form_tag new_publication_path()
Not necessarily the best way to do this, but you can hide some of the fields in the form depending on whether the model id is valid. For example (in haml):
- if #model.id #only shows up if the model has been saved.
= f.text_field :field_name, ...
This way you can use the usual new, and then when the model has been saved, just redirect to the 'edit' action and the rest of the fields show up.

Submitting custom information to a controller

This is probably simple, but I've tried a few things and couldn't find a way to make it work.
I would like to update a model with custom information given in a form_for
To make it more concrete, I'm on the show page for a particular instance of MyClass and I would like to pass something like the string "yay" into the controller, and then do as I please with the input. Maybe pass it back to the page as a flash message, or maybe modify the contents and then store it as a field of the MyClass instance.
I can write form_for's that contain the attributes of MyClass without prbolems, but it seems that other fields throw an error.
How do I write the form_for so that I can accomplish one of the two above scenarios?
def update
#my_class = MyClass.find(params[:id])
flash[:notice] = "This works" # but what can I write in a form for for it to be a variable that's passed in?
#rest of the update
end
Form helpers that unitize a form builder instance (like f.text_field) expect a valid model attribute so it can generate the appropriate id and populate the field with data from the model. If you want to have form fields that do not correspond to model attributes, don't use the the standard f.text_field but instead use:
<%= text_field_tag 'my_custom_tag' %>
which should render something like:
<input type="text" id="my_custom_tag"></input>
When the form is submitted, the value of the input will show up in the params hash with a key of :my_custom_tag.
I hope this helps.
It seems that you would probably need a hidden_field in your form :
http://apidock.com/rails/ActionView/Helpers/FormHelper/hidden_field
However, if you wish to save some kind of state, which seems like this is what you want, you would never use that. Instead, you would use a session. The reason is that a hidden field can be manipulated by the client and thus security can easily be overridden.
Like Spyros said, a hidden field will give you the place. Assuming you are ok with the fact that a user can modify the URL, just add attr_accessor :foo to your model.
In the controller you can access it with bar = params[:foo] and do as you please.

I want to pass the name of a calling web page in a form in Ruby on Rails

I am quite new to Ruby. I have a landing page controller and index page that has a button on it that pops up a user input form for email addresses, etc. One of the things I want to capture and write into the database is the name of the originating landing page.
For example:
www,mydomain.com/landngpage/campaign1
Another landing page could be:
www,mydomain.com/landngpage/campaign2
The above form calls a ppc_user controller
www,mydomain.com/lppc_user/new
Can anyone help me on this? I have seen a few examples of passing data using the flash option, but I can't get this to work.
I guess you're looking for request.referer.
It tells you from which page the user comes from.
You could use a hidden field and in fill it with an instance variable created in the controller...
so in your controller index:
def index
#campaign = params[:campaign] # this is whatever parameter you have named that is "campaign1", "campaign2", etc..
end
then in your form:
hidden_field :campaign, #campaign
or with the answer given by apneadiving:
hidden_field :campaign, request.referer
and then whatever controller you are posting your message to will have a param called :campaign containing the URI that it came from or the name of the campaign parameter depending on which one you choose to use.

Resources