Rails Forms - Edit User Form - ruby-on-rails

I am trying to create an Edit User Form to amend the user details stored in my database but keep getting error. I have changed the form URL and tried changing it to a method: :get without much success.
Edit User Form
<%= form_with url: #user, :class => "user_sign_in_form" do |form| %>
<%= form.text_field "user[email]", placeholder: "email", :class => "form_box" %>
<%= form.text_field "user[username]", placeholder: "username", :class => "form_box"%>
<%= form.text_field "user[password]", placeholder: "password", :class => "form_box" %>
<%= form.submit "SUBMIT" %>
<% end %>
Edit Action
def edit
#user = User.find(params[:id])
end
Link to Edit User Form
<%= link_to "Edit", edit_user_path(#user) %>
Error Message
No route matches [POST] "/users/58"

You need to use model: #user not url: #user.
<%= form_with model: #user, :class => "user_sign_in_form" do |form| %>
<%= form.text_field :email, placeholder: "email", :class => "form_box" %>
<%= form.text_field :username, placeholder: "username", :class => "form_box"%>
<%= form.password_field :password, placeholder: "password", :class => "form_box" %>
<%= form.submit "SUBMIT" %>
<% end %>
When you pass a model form_with will set both the action (/users or /users/:id) and method (POST or PATCH) depending on if the record has been saved. See Resource Routing: the Rails Default.
The url: option should really only be used if you have to override the conventional routes or if you have a form that does not wrap a model instance.
Also use form.text_field :email instead which will "bind" the input to the model attribute. Nobody likes having to refill an entire form from scratch because they missed some minor detail.
And on a side note do not use placeholders instead of labels - its an aweful practice from both a useability and accessibility standpoint.

Related

Make the Devise new user form created with railapps a partial to re use it

I've created a new app with the Rails App composer with Devise and Pundit. The composer has created a form to create new users (devise/registrations/new.html.erb) which i would like to make a partial so i can put it on other pages. The form is
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :role => 'form'}) do |f| %>
<h3>Sign up</h3>
<%= devise_error_messages! %>
<div class="form-group">
<%= f.label :email %>
<%= f.email_field :email, :autofocus => true, required: true, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password %>
<%= f.password_field :password, class: 'form-control' %>
</div>
<div class="form-group">
<%= f.label :password_confirmation %>
<%= f.password_field :password_confirmation, class: 'form-control' %>
</div>ate_field :date_of_birth, class: 'form-control' %>
</div>
<%= f.submit 'Sign up', :class => 'button right' %>
<% end %>
if i put this into a partial and try to render it on another page i get
undefined local variable or method `resource
and that's obviously because in my controller i didn't define anything. Problem is i can't find the controller or what resource should be in the other controller so that i can re-use this.
The resource method you are looking for is located here
resource seems to be defined by an instance variable with the name of the actual resource you're using devise with.
So, if you use devise for the users resource you should define resource as User.new on the controller you want to render that partial, and mark it as helper_method so it may be used in views, or just define it straight in the helper.
Alternatively, you can just inherit DeviseController which already includes the resource definition and other useful methods as well.

update user in another controller rails

In my rails app, if I go to localhost:3000/users/edit to update user
<h2>Edit <%= resource_name.to_s.humanize %></h2>
<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
<%= devise_error_messages! %>
<div><%= f.label :email %><br />
<%= f.text_field :email%></div>
<div><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password, :autocomplete => "off" %></div>
<div><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %></div>
<div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
<%= f.password_field :current_password %></div>
<div><%= f.submit "Update" %></div>
<% end %>
<h3>Cancel my account</h3>
<p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), :data => { :confirm => "Are you sure?" }, :method => :delete %></p>
<%= link_to "Back", :back %>
It 's alright, but if I move this code ((or render this view) to another view of another controller it 'll have an error :"undefined local variable or method `resource' for #<#:0x3b27478>"
I don't know how to fix it, any help really appreciated.
resource is a variable set by the Devise gem. In order to move the above code somewhere else, it means that you will have to take care of setting the resource variable yourself.
Basically a rails form_for wants to take the instance of the object you want to create / edit.
<%= form_for instance_object ...
In your case you'll need to first fetch the user you want to edit (or use current_user) and then give it to the form_for helper method:
<%= form_for current_user ...
or by setting the #user instance variable in your controller first:
def new
#user = User.new
end
def edit
#user = User.find(some_id)
end
Then in your view:
<%= form_for #user ...
Don't forget to replace all the resource occurences by your instance variable.

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.

send email to multiple email address rails 3

I am a user model and in my view I have:
#user = current_user
User model have an attribute with name "email".
and I want send one e-mail to multiple email address with a subject.
I have a form like:
<%= form_for (Email.new), :method => :post, :remote => true, :url => { :controller => "users", :action => "invite_friends" } do |f| %>
<%= f.text_field :address1 %>
<%= f.text_field :address2 %>
<%= f.text_field :address3 %>
<%= f.text_field :address4 %>
<%= f.text_area :subject_email %>
<% end %>
Have I that create a "email model" with attributes address and subject_email?
Check section 2.3.3 Sending Email To Multiple Recipients from the Rails Guide

Fill rails form conditionally

I'm making a simplistic message board with tags. The message#index view displays a list of all messages. The tag#show view shows messages of a specified tag. On the message#index view, there is a form (partial) that requires the user to write a message and to tag it. On the tag#show view, I'd like to use the same form partial but to have the view's tag automatically filled into the form. In the show action of the tags controller, the name of the tag is #title.
The form partial looks like this:
<% form_for :message, :url => { :action => "create" }, :html => { :id => 'form' } do |f| %>
<%= f.error_messages %>
<%= f.label :tag, "tag<p2>( separate tags with a comma )</p2>" %>
<%= f.text_field :tag_list %>
<%= f.label :name, "name<p2>( optional )</p2>" %>
<%= f.text_field :name %>
<%= f.label :email, "email<p2>( optional )</p2>" %>
<%= f.text_field :email %>
<%= f.label :title, "message" %>
<%= f.text_area :content %>
<%= f.submit 'submit' %>
<% end %>
How do I auto fill the tag_list text field with the #title value? #title is a string. I appreciate any help you can offer. Thank you.
<%= f.text_field :tag_list, :value => #title %>

Resources