Rails params[:email] always returning nil - ruby-on-rails

I have the following simple rails page:
<h1> New User </h1>
<%= form_for :user, url:users_path do |f| %>
<p>
<%= f.label :email %>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %>
<%= f.text_field :password %>
</p>
<%= f.submit %>
<% end %>
The create action gets executed and I want to access the :email attribute.
def create
render text: params[:email].inspect
end
The above always displays nil.

form_for :user will place all parameters beneath a :user key
render text: params[:user][:email].inspect

Related

rails - Getting an id from the url and then passing it as an argument for creating an object

First off I would like to ask how to redirect after clicking the submit button in a creation form. I have a form for creating a quiz and after they hit submit I want to redirect them to the link 'quiz/:id/add_question' where :id is the id of the just created quiz, but instead I just get redirected to /quizzes and it leads to a "The connection was reset" error in my browser.
Here is my quiz form:
<div>
<h2>Create Quiz</h2>
<%= form_for(#quiz) do |f| %>
<div>
<%= f.label :name %>
<%= f.text_field :name %> </br>
<%= f.label :subject %>
<%= f.text_field :subject %>
</div>
<%= f.submit "Submit" %>
<% end %>
</div>
And my quizzes controller:
class QuizzesController < ApplicationController
def new
#quiz = Quiz.new
end
def create
#quiz = Quiz.new(quiz_params)
flash[:success] = "Quiz Created successfully" if #quiz.save
redirect_to 'quiz/' + #quiz.id.to_s + '/add_question'
end
private
def quiz_params
params.require(:quiz).permit(:name, :subject)
end
end
The quiz/:id/add_question link leads to a question creation form:
<div>
<%= form_for(#question) do |f| %>
<%= f.label :question %></br>
<%= f.text_field :question %></br>
<%= f.label :answer1 %></br>
<%= f.text_field :answer1 %></br>
<%= f.label :answer2 %></br>
<%= f.text_field :answer2 %></br>
<%= f.label :answer3 %></br>
<%= f.text_field :answer3 %></br>
<%= f.label :answer4 %></br>
<%= f.text_field :answer4 %></br>
<%= f.label :correct_id %></br>
<%= f.text_field :correct_id %></br>
<%= f.submit "Add question" %>
<% end %>
</div>
The other part of my question is how can I pass another argument for the creation of the question object here. I don't want the user to enter that argument because the argument should be the id from the current url (quiz/:id/add_question).
First of all i think you made a spelling error:
redirect_to 'quiz/' + #quiz.id.to_s + '/add_quesiton'
Should be
redirect_to 'quiz/' + #quiz.id.to_s + '/add_question'
But rather you should use the url helper that is available.
redirect_to quiz_add_question_path(#quiz.id)
Assuming thats the name of the route. You can find that name by running rake routes
For the id of the current quiz you can set a hidden attribute for example
<%= f.hidden_field :quiz_id, value: params[:id] %>

Converting form tag to simple form

How would I go about converting this form tag below into a form_for?
<%= form_tag(contact_email_path, :method => 'post') do %>
<%= label_tag "Your email" %>
<%= text_field_tag "sender", #sender, :autofocus => true %>
<%= label_tag "Subject" %>
<%= text_field_tag "subject", #subject %>
<%= label_tag "Message" %>
<%= text_area_tag "message", #message %>
<%= submit_tag "Send Email" %>
<% end %>
form_for is a helper for creating forms which create or edit a resource.
If you have a resource here that you would like to create in your database, you would use this method. What it looks like you're doing here is not creating a resource, but sending an email. If that is the case, then a form_tag is probably a better option.
If you are, however, trying to create a new resource in the database (i.e. an new instance of ContactEmail or some other class), then you could do it like this:
<%= form_for #contact_email do |f| %>
<%= f.label :sender, "Your email" %>
<%= f.text_field :sender, :autofocus => true %>
<%= f.label :subject %>
<%= f.text_field :subject %>
<%= f.label :message %>
<%= f.text_area :message %>
<%= f.submit "Send Email" %>
<% end %>
This assumes that #contact_email is an object that has the methods sender, subject and message and that you have resources :contact_email in your routes file.

Cant assign value to input

I have this form:
<%= form_for(Imagedocu.new) do |f| %>
<%= f.text_area :image %>
<%= f.text_field :patient_id, params[:id]%>
<%= f.text_field :type %>
<%= f.submit %>
<% end %>
How you can see i assign a value to one of the inputs:
<%= f.text_field :patient_id, params[:id]%>
I dont know why but now somehow i get the error:
undefined method `merge' for "73539":String
73539 is the params[:id]! What do i wrong?
This is a form that already contains an object (Imagedocu.new), so it "grabs" the value for patient_id directly from this object. If you want to overwrite this value, use this:
<%= f.text_field :patient_id, value: params[:id] %>
OR initialize the Imagedocu.new with a patient_id equal to params[:id]:
<%= form_for( Imagedocu.new(patient_id: params[:id]) ) do |f| %>
<%= f.text_area :image %>
<%= f.text_field :patient_id %>
<%= f.text_field :type %>
<%= f.submit %>
<% end %>
But a cleaner way is to initialize the new Imagedocu object in the controller's action:
# controller
def new
#imagedocu = Imagedocu.new(patient_id: params[:id])
# etc.
# view
<%= form_for( #imagedocu ) do |f| %>
# etc.

Rendering devise error messages in scoped custom form

I have to Devise models, User and Vendor. I generated the views for Vendor and customized the signup form to suit my needs. I needed to tweak the registrations controller for Vendor, like so:
controllers/vendors/registrations_controller.rb
class Vendors::RegistrationsController < Devise::RegistrationsController
skip_before_filter :require_no_authentication
def create
super
end
protected
def sign_up(resource_name, resource)
true
end
def new
super
end
end
My signup view for Vendor looks like so:
views/vendors/registrations/new.html.erb
<h2>Sign up</h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => {:multiparet => :true} ) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :store_name %>
<%= f.text_field :store_name %></div>
<div><%= f.label :contact_name %>
<%= f.text_field :contact_name%></div>
<div><%= f.label :contact_phone %>
<%= f.text_field :contact_phone %></div>
<div><%= f.label :address %>
<%= f.text_field :address %></div>
<div><%= f.label :image %>
<%= f.file_field :image %></div>
<div><%= f.label :email %>
<%= f.email_field :email, :autofocus => true %></div>
<div><%= f.label :password %>
<%= f.password_field :password %></div>
<div><%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation %></div>
<div><%= f.submit "Sign up" %></div>
<% end %>
<%= render "vendors/shared/links" %>
When I attempt to render the page, I get this error:
undefined method `errors' for nil:NilClass
How can I render devise error messages in this situation? Thanks in advance!
I would write this as a comment, but it will be easier to read as an answer:
Perhaps you could try replacing devise_error_messages! with a standard Rails error display:
<% if resource.errors.any? %>
<div class="error">
<strong><%= pluralize(resource.errors.count, "Error") %></strong>
<ul>
<% resource.errors.each do |attr,msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

Form for related model not showing up

I am trying to have a form show up for a related model, but it is not displaying when I view the page in a browser. How do I url field in my fields_for to display?
Here is my code:
User Model:
class UsersController < ApplicationController
def new
#user = User.new
#user.websites.build
end
def create
#user = User.new(params[:user])
if #user.save
redirect_to root_url, :notice => "Signed up!"
else
render "new"
end
end
end
Website Model:
class Website < ActiveRecord::Base
belongs_to :user
end
Users View:
<h1>Sign Up</h1>
<%= form_for #user do |f| %>
<% if #user.errors.any? %>
...
<% end %>
<p>
<%= f.label :email %><br/>
<%= f.text_field :email %>
</p>
<p>
<%= f.label :password %><br/>
<%= f.password_field :password %>
</p>
<p>
<%= f.label :password_confirmation %><br/>
<%= f.password_field :password_confirmation %>
</p>
<% f.fields_for :websites do |builder| %>
<%= builder.label :url %><br/>
<%= builder.text_field :url %>
<% end %>
<p class="button"><%= f.submit %></p>
<% end %>
Final output:
You missed the equals sign in your erb tag.
<% f.fields_for :websites do |builder| %>
.. should be ..
<%= f.fields_for :websites do |builder| %>
Does that fix it?
It looks like you're maybe confusing singular and plural in your fields_for as well. Calling it with the plural websites then treating the block as a singular website doesn't make sense.

Resources