Devise invitable show inviter name or email - ruby-on-rails-3

I want to be able to show the user that sent out the invitation rather than just my domain when sending out a devise invitation but haven't been able to find any documentation on this.
The two places where I need to show this name are in the invitation email-
(in place of 'Someone')
<p>Hello <%= #resource.email %>!</p>
<p>Someone has invited you to <%= root_url %>, you can accept it through the link below.</p>
<p><%= link_to 'Accept invitation', accept_invitation_url(#resource, :invitation_token => #resource.invitation_token) %></p>
<p>If you don't want to accept the invitation, please ignore this email.<br />
Your account won't be created until you access the link above and set your password.</p>
and the set password page.
<h4>You're seeing this page because someone has invited you to the site</h4>
<%= simple_form_for resource, :as => resource_name, :url => invitation_path(resource_name), :html => { :method => :put } do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :invitation_token %>
<div class="row">
<div class="signup_well span3 offset1">
<legend><%= t 'devise.invitations.edit.header' %></legend>
<%= f.input :password %>
<%= f.input :password_confirmation %>
<%= hidden_field_tag :token_key, resource.invitation_token %>
<%= f.submit t("devise.invitations.edit.submit_button") %>
<% end %>
</div>
I may over looking any documentation on the best approach to do this. Your help saves a lot of frustration. Thank you.

This should works: <%= #resource.invited_by.first_name %>.

Related

Registration in Rails

I am attempting to create a new user in rails. There is a username, email, password and password confirm field on the new user form. When I click on the create user button on the web page, the new_user form simply refreshes and the user is not added to the database.
Here is the code for my register method in the authentication controller
def register
#user = User.new(params[:user])
if #user.valid?
#user.save
session[:user_id] = #user.id
flash[:notice] = 'Welcome.'
redirect_to sign_in_url
else
render :action => "new_user"
end
end
This is my new_user form:
<p>Sign Up</p>
<%= form_for #user, :as => :user, :url => new_user_path, :method => :put do |f| %>
<p>
<%= f.label 'username:' %><br/>
<%= f.text_field :username %>
<%= show_field_error(#user, :username) %>
</p>
<p>
<%= f.label 'email:' %><br/>
<%= f.text_field :email %>
<%= show_field_error(#user, :email) %>
</p>
<p>
<%= f.label 'password:' %><br/>
<%= f.password_field :password %>
<%= show_field_error(#user, :password) %>
</p>
<p>
<%= f.label 'password confirmation:' %><br/>
<%= f.password_field :password_confirmation %>
<%= show_field_error(#user, :password_confirmation) %>
</p>
<p>
<%= f.submit 'Sign Up' %>
<%= f.submit 'Clear Form', :type => 'reset' %>
</p>
Can anybody point me in the right direction?
Your call to new_user_path in your form references the url to User#new if you have defined resources :users in your routes.rb file. You will need to replace new_user_path with register_user_path, if you have defined register in your routes file. Double check for the path with by running rake routes, though.
From what I can see, there is no need to use have the register action. It'd make more sense to use RESTful actions new, create, index, show, edit, update, and destroy in your case, since your logic follows the intuition behind them. Rails makes this convention easy to follow. See the routing Rails guide here for more.

Two Form_For on One Model

I have the Following
Page Model
Client Model
Business Model
There is No Relationship between any of these Models.
I would like to Have on the Show Template of the Page to have the Form_for's
One for the Client
One for the Business
Is This Possible?
Current Have The Following:
<div id="sidebar">
<%= form_for (#business) do |f| %>
<div id="contact_form_name">
<p>Company</p>
<%= f.text_field :company_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
<div id="sidebar">
<%= form_for (#client) do |f| %>
<div id="contact_form_name">
<p>First Name</p>
<%= f.text_field :first_name, :class =>'form_input_small' %>
<%= f.submit 'Submit', :class => 'button' %>
<% end %>
</div>
Error I am Getting in the Log is the following
<div id="sidebar">
78: <%= form_for (#business) do |f| %>
79: <div id="contact_form_name">
80: <p>Company</p>
81: <%= f.text_field :company_name, :class =>'form_input_small' %>
app/views/pages/show.html.erb:78:in `_app_views_pages_show_html_erb___1556847543_65073939124600'
app/controllers/pages_controller.rb:9:in `show'
Routes
businesses GET /businesses(.:format) {:action=>"index", :controller=>"businesses"}
POST /businesses(.:format) {:action=>"create", :controller=>"businesses"}
new_business GET /businesses/new(.:format) {:action=>"new", :controller=>"businesses"}
edit_business GET /businesses/:id/edit(.:format) {:action=>"edit", :controller=>"businesses"}
business GET /businesses/:id(.:format) {:action=>"show", :controller=>"businesses"}
PUT /businesses/:id(.:format) {:action=>"update", :controller=>"businesses"}
DELETE /businesses/:id(.:format) {:action=>"destroy", :controller=>"businesses"}
Rails doesn't really care where you have your forms so long as you provide it with the necessary information, & there's nothing that says you can't mingle various models together into a single view.
Assuming you're making use of RESTful resources(as you should), you'll have something like:
resources :pages
resources :companies
resources :clients
Setup in your routes.rb, this makes it pretty easy to specify how you want your form_fors to operate.
For instance on your show action for your Page model you could have something like:
<h1>New Company:</h1>
<%= form_for #company, :url => companies_path do |f| %>
...
<% end %>
<h1>New Client:</h1>
<%= form_for #client, :url => clients_path do |f| %>
...
<% end %>
Make sure you're setting the instance variables #company and #client in you pages controller show action like #company = Company.new & #client = Client.new.
In both of these cases your forms will post to the create action of their respective models. You can check out relying on record identification for further reading.

Ruby on Rails Tutorial chapter 9

I am confused by the addition of [:session] to params. It also seems to break my website. Can someone please explain what this does for me?
class SessionsController < ApplicationController
.
.
.
def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
.
.
.
end
Error message:
1) Authentication signin with invalid information
Failure/Error: before { click_button "Sign in" }
NoMethodError:
undefined method []' for nil:NilClass
# ./app/controllers/sessions_controller.rb:7:increate'
# (eval):2:in click_button'
# ./spec/requests/authentication_pages_spec.rb:18:inblock (4 levels) in '
EDIT 8/2
I believe the problem is related to a switch from form_for to form_tag. I lost the reference to sessions in the switch because I could not figure out how to properly include it. If anyone has any advice on this issue it would be most appreciated. I am wondering if there is a practical reason for wanting it to be params[:session][:email] instead or is it just for organization?
new.html.erb
<% provide(:title, "Sign in") %>
<h1>Sign in</h1>
<div class="row">
<div class="span6 offset3">
<%= form_tag sessions_path do %>
<%= label_tag :email %>
<%= text_field_tag :email %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
<p>New user? <%= link_to "Sign up now!", signup_path %></p>
</div>
</div>
Try removing the [:session] brackets, that worked for me
It brakes your code 'cos the params[:session] is nil I think and you trying to get [:email] from nil, what ofcourse should cause the exception. There should be some code in tutorial that defines params[:session] hash. Try to look better.
To make your code stable you have to be sure that params[:session] is always defined or try to use ternar function params[:session] ? params[:session][:email] : ''
You can replace this in your view :
<%= form_for :session, :url => sessions_path do %>
<%= label_tag :email %>
<%= text_field_tag :email %>
<%= label_tag :password %>
<%= password_field_tag :password %>
<%= submit_tag "Sign in", class: "btn btn-large btn-primary" %>
<% end %>
form_tag generate just an HTML form tag and form_for is used to describe something. All the inputs for a field of a form create with form_for will have a name like this : user_session[email]. So, when you submit the form, in your controller, you will have this : params[:user_session][:email].

ruby on rails form and passing its parameter to controller

When the view pass the parameters to controller,
controller gets nil for all of the arguements somehow.
Can anyone how to fix this?? Thanks!
and I have no model called "Message"
controllers/messages_controller.rb
def deliver
recipient = User.find_by_username(params[:recipient])
subject = params[:subject]
body = params[:body]
current_user.send_message(recipient, body, subject)
redirect_to :controller => 'messages', :action => 'received'
flash[:notice] = "message sent!"
end
views/messages/new.html.erb
<%=form_for :messages, url: url_for( :controller => :messages, :action => :deliver ) do |f| %>
<div class="field">
<%= f.label :subject %><br />
<%= f.text_field :subject %>
</div>
<div class="field">
<%= f.label :body %><br />
<%= f.text_field :body %>
</div>
<div class="actions">
<%= f.submit %>
<% end %>
Check your source HTML to better understand what FormHelpers do.
With the form_for f.text_field will generate names attributes in the format:
messages[subject]
Consequently, your params will be in the format:
params[:messages][:subject]
You can also use <%= debug params %> to inspect what's in params, it's very helpful.
You can get parameter value using datas = params[:messages]
These values are in array form. So you can fetch array datas If you want to individual data then usesubject = datas[:subject]
body = datas[:body]
To check run following code in view
<%= subject %>
this gives the value of subject.

rails - confused about routing and params

I have a two models, Group and User
User belongs_to Group and
Group has_many Users
In my groups/show.html.erb I have the user sign-up form
<h1>Create user</h1>
<%= form_for #user do |f| %>
<%= render 'shared/error_messages', :object => f.object %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
To get a relation between the Group and the User I have a create method in my Users controller as follows
def create
#group = Group.find(params[:group][:id])
#user = #group.users.build(params[:user])
if #user.save
flash[:success] = "You have created a new user"
redirect_to group_path
else
#title = "Create user"
render 'new'
end
end
I have also tried:
#group = Group.find(params[:id])
and
#group = Group.find(params[:group_id])
But I still get an error
Essentially I want to create new users in the group/show.html.erb and associate that user with the group where the user was created. If the user is created in groups/3 for example, how do I set my create method in the Users controller to make sure this relation holds?
In general I've been following the Hartl Rails tutorial book at http://ruby.railstutorial.org/chapters/user-microposts#sec:creating_microposts and following the approach for forms and create methods. However I am not sure how to get the params for groups/3 into the find method like #group = Group.find(?????)
Can someone please enlighten me, this issue has been bothering me for a few days now.
Thanks in advance
After the form is submitted, it takes you to the users#create. This route doesn't have a group_id segment.
To pass group_id there, you need to store it in a hidden field in your form.