DRY form partial for create and update - ruby-on-rails

I have a _form.html.erb form partial which helps to DRY up my code but I need the form to have different labels depending on if I am creating a new user or updating an existing user.
Here is my form partial. I don't need to show the eula checkbox during update and I also need to replace the "Create my account" submit button text to something more appropriate when doing an update.
<% form_for #user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, 'Full name' %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, 'Email address' %><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>
<p>
<%= f.check_box :eula %>
<%= f.label :eula, 'I agree to the terms and conditions' %>
</p>
<p><%= f.submit "Create my account" %></p>
<% end %>
Which one of the following is the best way to do this?
have 2 separate form partials, one for create and one for update
have 1 form partial but have conditional labels based on the action (is this possible?)
factor the common part into a partial and reuse that in the create and update forms
If I were to do conditional form how would I check which action is being performed?

ActiveRecord has the new_record? method which you can use to decide what to show on the form:
<% form_for #user do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name, 'Full name' %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.label :username %><br />
<%= f.text_field :username %>
</p>
<p>
<%= f.label :email, 'Email address' %><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>
<% if #user.new_record? %>
<p>
<%= f.check_box :eula %>
<%= f.label :eula, 'I agree to the terms and conditions' %>
</p>
<% end %>
<p><%= f.submit #user.new_record? ? "Create my account" : "Update my account" %></p>
<% end %>

Wrap the <form> tag around the call partial tag and put the submit button in the respective views. Only put the eula check box in the create view.
You can create a variable in the new and update views and use that as your label name.
<%= f.label email, emaillabel %>
[Edit]
If you need to pass variables to a partial use this:
<%= render :partial => 'form', :locals => { :myvar => myvar } %>

Related

appropriate usage of hidden_fields

I am sending one parameter ("designer" or "developer") to register form prepared by Devise and I want to add this parameter to devise model.
My question is if assigning parameter to hidden_field is appropriate solution.
From this view I'm redirecting to user registration form
<%= link_to "Register as Owner", new_user_registration_path(:role => 'owner') %>
<%= link_to "Register as Employee", new_user_registration_path(:role => 'employee' ) %>
User registration form
<%= form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
<%= devise_error_messages! %>
<%= f.hidden_field :role, :value => params[:type]%>
<div class="field">
<%= f.label :email %><br />
<%= f.email_field :email, autofocus: true %>
</div>
<div class="field">
<%= f.label :password %>
<% if #minimum_password_length %>
<em>(<%= #minimum_password_length %> characters minimum)</em>
<% end %><br />
<%= f.password_field :password, autocomplete: "off" %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation, autocomplete: "off" %>
</div>
<div class="actions">
<%= f.submit "Sign up" %>
</div>
<% end %>
As long as the param doesn't contain sensitive information (passwords etc) as you say it doesn't, there isn't a problem with your implementation.

No method error in Users#edit

I am learning ruby on rails. I want to make editable users account.
I have an error in localhost:
undefined method `model_name' for NilClass:Class
This is my edit.html.erb file:
<h1>Edit 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 "Update" %>
</div>
<% end %>
<div>
<%= gravatar_for #user %>
change
</div>
Can you provide a solution?
Most likely your #user object is nil, so when it tries to call #user.model_name, it fails. You should check your controller logic for loading it, and make sure it finds a valid #user.

How do you call a from partial from different model in a layout/header

In Rails how do I call a form from another model in any given layout? I have a login form I want to put in the header of every page. I created a partial with the following in it:
<% form_for(#user_session) do |f| %>
<p>
<%= f.label :username %><br />
<%= f.text_field :username, :class=>'' %>
</p>
<p>
<%= f.label :password %><br />
<%= f.password_field :password %>
</p>
<%= f.check_box :remember_me %><%= f.label :remember_me %><br />
<br />
<p>
<%= f.submit 'Login' %>
</p>
<% end %>
then tried calling that partial in my header and that doesn't seem to work.
<%= render :partial => 'user_sessions/login' %>
I get a "Called id for nil" error
<%= render :partial=> 'user_sessions/login' :layout => false%>
Use this
I think you save your partial with '_login.rhtml' like ?
Why not just <%= render #user_session %> ?
Is the #user_session variable set up within all of your controller actions?
Got the answer from jmesserer over at railsforum, just needed to change:
<% form_for(#user_session) do |f| %>
to
<% form_for UserSession.new do |f| %>

Show or hide fields depending on the Acl9 role - Ruby on Rails

I am using Acl9 to manage the roles and I want to hide the checkbox usertype if the user has the role :customer and show it if the role is :manager. I want that just the :manager can edit all the fields and some for the :customer.
Thank you for your help!
<h1>Editing user</h1>
<% form_for(#user) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :usertype %><br />
<%= f.check_box :usertype %>
</p>
<p>
<%= f.label :surname %><br />
<%= f.text_field :surname %>
</p>
<p>
<%= f.label :firstname %><br />
<%= f.text_field :firstname %>
</p>
<p>
<%= f.label :phone %><br />
<%= f.text_field :phone %>
</p>
<p>
<%= f.label :email %><br />
<%= f.text_field :email %>
</p>
<p>
<%= f.label :registrationdate %><br />
<%= f.datetime_select :registrationdate %>
</p>
<p>
<%= f.label :login %><br />
<%= f.text_field :login %>
</p>
<p>
<%= f.label :password %><br />
<%= f.text_field :password %>
</p>
<p>
<%= f.submit 'Update' %>
</p>
<% end %>
<%= link_to 'Show', #user %>
<%= link_to 'Back', users_path %>
You can also do think like that:
..in your application_helper.rb
module ApplicationHelper
include Acl9Helpers
end
..and in your views, something like that
<% show_to(:admin) do %>
The content to show
<% end %>
According to the Acl9 documentation, you should be able to do something like this:
<% if #user.has_role?(:manager, nil) %>
<p>
<%= f.label :usertype %><br />
<%= f.check_box :usertype %>
</p>
<% end %>

Basic Ruby On Rails Linking Help

So I am beginning to work with Rails and I get some of the concepts but am stuck on an important one.
Let's say I have customers which has many jobs and jobs which belongs to customers.
How would I go about creating a new job for a customer?
I can create a link that goes to customers/1/jobs/new and I can grab the customer ID but how do I tell it that I am creating a job for customer 1?
I know this is the most basic of things but I just need a push in the right direction.
This is my form so far:
How do I get :customer_id to populate with the customer_id param?
<h1>New job</h1>
<% form_for(#job) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :customer_id %><br />
<%= f.text_field :customer_id %>
</p>
<p>
<%= f.label :manufacturer %><br />
<%= f.text_field :manufacturer %>
</p>
<p>
<%= f.label :serial_number %><br />
<%= f.text_field :serial_number %>
</p>
<p>
<%= f.label :problem %><br />
<%= f.text_area :problem %>
</p>
<p>
<%= f.label :notes %><br />
<%= f.text_area :notes %>
</p>
<p>
<%= f.label :status %><br />
<%= f.text_field :status %>
</p>
<p>
<%= f.label :tech_id %><br />
<%= f.text_field :tech_id %>
</p>
<p>
<%= f.submit 'Create' %>
</p>
<% end %>
<%= link_to 'Back', jobs_path %>
Just use form_for([#customer, #job]), this should generate the correct URLs (/customers/:customer_id/jobs etc).
You can then fetch params[:customer_id] in your JobsController.create method.
Here's a long, maintained, descriptive list of good ruby programming tutorials. Good luck.

Resources