Rails Gem: How to get a gem application layout? - ruby-on-rails

I'm trying to customize a Rails Gem with the default CRUD-Views.
app/views/new.html.erb:
<div class="wishlist">
<div class="row">
<div class="small-12 medium-10 large-8 small-centered columns">
<h1><%= Spree.t(:creating_wishlist) %></h1>
<%= form_for #wishlist do |f| %>
<p><%= f.label :name, Spree.t(:name) %>: <%= f.text_field :name %></p>
<p><%= f.check_box :is_private %> <%= f.label :is_private, Spree.t(:is_private) %></p>
<%= f.submit Spree.t(:create) %>
<% end -%>
</div>
</div>
</div>
I want to avoid the same div containers .wishlist .row etc. in all the other views.
How can i achieve something like:
# gem application layout:
<div class="wishlist">
<div class="row">
<div class="small-12 medium-10 large-8 small-centered columns">
<%= yield %>
</div>
</div>
</div>
When i save it as app/views/layouts/application.html.erb and render it in the gem controller with layout 'application' then the view of the main Rails Application Layout will be overwritten.
But I just want only the Gem CRUD views to get yield.

I could solve it with a partial that i render outside a do/end-block of the CRUD Views:
partial in app/views/base/_layout.html.erb
<div class="wishlist">
<div class="row">
<div class="hide-for-small medium-1 large-2 columns"></div>
<div class="small-12 medium-10 large-8 small-centered columns">
<%= yield %>
</div>
<div class="hide-for-small medium-1 large-2 columns"></div>
</div>
</div>
and in my CRUD Views:
<%= render :layout => 'base/layout' do %>
# content
<% end -%>

Related

'nil' is not an ActiveModel-compatible object. It must implement :to_partial_path in rails 7

implementing a <%= render #booking_types %> partial in my home#dashboard view, Why is rails not looking into app/views/booking_types folder for a partial named _booking_type.html.erb and renders it as a looped instance?
app/views/home/dashboard.html.erb
<div class="flex items-center justify-between pb-6 border-b">
<h1 class="font-extrabold text-3xl">Dashboard</h1>
<%= link_to "Create booking type", new_booking_type_path, class: button_class(theme: "primary") %>
</div>
<div class="py-10 grid grid-cols-3 gap-6">
<%= render #booking_types %>
</div>
</div>
the partial
app/views/booking_types/_booking_type.html.erb
<div id="<%= dom_id booking_type %>">
<%= link_to edit_booking_type_path(booking_type), class: "p-6 border shadow-xl rounded-xl block hover:shadow-lg transition ease-in duration-300 relative overflow-hidden" do %>
<div class="h-1 w-full absolute top-0 left-0 right-0" style="background-color: <%= booking_type.color %>">
</div>
<h3 class="font-medium text-2xl"><%= booking_type.name %></h3>
<p class="text-gray-500">View booking page</p>
<p><%= duration(booking_type) %></p>
<% if booking_type.payment_required? %>
<div class="mt-3 pt-3 border-t">
<p>
<strong>Payment required</strong>
</p>
<p>
<strong>Price:</strong>
<%= number_to_currency(booking_type.price) %>
</p>
</div>
<% end %>
<% end %>
</div>
Error Output by better-errors gem after changing rendering partial from <%= render #booking_types %> to this way
<%= render "booking_types/booking_type" %>

Submitting two forms in Rails sharing a same param

I have a form like that:
<%= form_for #report do |f| %>
<div class="row">
<div class="col-sm-9">
<h3 class="page-header">
Children
</h3>
<div class="row">
<% students.each do |student| %>
<div class="col-xs-4 col-md-2">
<div class="std_container">
<%= check_box_tag "student_ids[]", student.id, nil, {id: "check_#{student.id}", class: "student_check"} %>
<%= label_tag "check_#{student.id}" do %>
<div class="std_label"><%= student.name %></div>
<div class="std_img thumbnail"><img src="http://www.codeproject.com/KB/GDI-plus/ImageProcessing2/img.jpg" alt=""></div>
<% end %>
</div>
</div>
<% end %>
</div>
</div>
<%= render "form_categories", f: f, report_notes: #report.report_notes %>
<%= render "messages/form", ????? %>
</div>
</div>
</div>
<% end %>
And I need to render these two partials on the bottom. The 'messages' partial is a form that responds to a different controller but needs to use the "student_ids[]" parameter present on the #report form together with its own parameters. This partial is:
<%= form_for #message do |f| %>
<div class="row">
<div class="col-sm-10 col-sm-offset-1">
<%= f.hidden_field :professor_id, :value => current_user.professor.id %>
<%= f.label :text, "Texto" %>
<%= f.text_area :text %>
<%= f.submit "Enviar", class: "btn btn-default" %>
</div>
</div>
<% end %>
How do build this "messages" partial in a way that I can use the "student_ids[]" and submit it to its controller?
It is not possible. I solved this problem by using javascript.

Rounded avatar ruby on rails

I would like to have my avatars that my users upload to be round on my rails app project for Udemy. The image still comes out square the way it was uploaded.
below is my show.html.erb file
<div class="row">
<div class="col-md-3 text-center">
<div class="avatar"><%= image_tag #user.profile.avatar.url %></div>
</div>
<div class="col-md-6">
<h1><%= #user.profile.first_name %> <%= #user.profile.last_name %></h1>
<h3><span class="job_title_icon"><%= job_title_icon %></span> <%= #user.profile.job_title %></h3>
<div class="well profile-block profile-description">
<h3>Background</h3>
<%= #user.profile.description %>
</div>
<% if current_user.plan_id == 2 %>
<div class="well profile-block contact-info">
<h3>Contact:</h3>
<%= #user.profile.phone_number %><br/>
<%= #user.profile.contact_email %><br/>
</div>
<% end %>
</div>
Below is my users,css.scss file
<div class="row">
<div class="col-md-3 text-center">
<div class="avatar"><%= image_tag #user.profile.avatar.url %></div>
</div>
<div class="col-md-6">
<h1><%= #user.profile.first_name %> <%= #user.profile.last_name %></h1>
<h3><span class="job_title_icon"><%= job_title_icon %></span> <%= #user.profile.job_title %></h3>
<div class="well profile-block profile-description">
<h3>Background</h3>
<%= #user.profile.description %>
</div>
<% if current_user.plan_id == 2 %>
<div class="well profile-block contact-info">
<h3>Contact:</h3>
<%= #user.profile.phone_number %><br/>
<%= #user.profile.contact_email %><br/>
</div>
<% end %>
</div>
I think setting your avatar class so that the image is round is a better way to approach it.
In your CSS, you define it as such:
.avatar {
border-radius: 50%;
// also might be good to set width and height
}
It looks as if you might be using Twitter Bootstrap. If so, there is a handy CSS class built in.
Just add class: 'img-circle' to your image_tag
...
#change this line as follows
<div class="avatar"><%= image_tag #user.profile.avatar.url, class: 'img-circle' %></div>
...
This page of the Bootstrap docs has lots of useful info and examples.

Missing partial invoices/__invoice_item_fields

i have invoice and invoice_items models in my application. and i have used cocoon gem for nested models.i am using rails 4.2. it is working properly when i am creating new invoice, but when i am clicking on my edit button i am getting "template missing error" though i have _invoice_item_fields.html.erb file in my application.
this is my _form.html.erb file
<div class=" form">
<%= form_for(#invoice,:html=>{:class=>"cmxform form-horizontal tasi-form",:novalidate=>"novalidat"}) do |f| %>
<% if #invoice.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#invoice.errors.count, "error") %> prohibited this invoice from being saved:</h2>
<ul>
<% #invoice.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group ">
<%= f.label :billing_address ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.text_area :billing_address,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :shipping_address ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.text_area :shipping_address,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :company_id ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.number_field :company_id,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :invoice_date ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.date_select :invoice_date,:class=>"form-control" %>
</div>
</div>
<div class="form-group ">
<%= f.label :status ,:class=>"control-label col-lg-2" %>
<div class="col-lg-6">
<%= f.number_field :status,:class=>"form-control" %>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
Invoice Items
<span class="pull-right">
<%= link_to_add_association 'Add Item', f, :invoice_items,:class=>"btn btn-default"%>
</span>
</header>
<div class="panel-body">
<div class="adv-table">
<%= f.fields_for :invoice_items do |item| %>
<%= render '_invoice_item_fields', :f => item %>
<% end %>
</div>
</div>
</section>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<%= f.submit "Save",:class=>"btn btn-danger"%>
</div>
</div>
<% end %>
</div>
this is my _invoice_item_fields.html.erb file
This...
<%= render '_invoice_item_fields', :f => item %>
must be
<%= render 'invoice_item_fields', :f => item %>
You do not use a leading _ in your Rails render call to render a partial. The underscore goes on the filename on disk only.

Routing problems with STI

I've declared an STI on Works
class Work < ActiveRecord::Base
validates :title, presence: true
validates :visibility, presence: true
belongs_to :category
end
class Story<Work
end
class Poem<Work
end
Code for form:
<div class="panel panel-blue margin-bottom-40">
<div class="panel-heading">
<h3 class="panel-title"><i class="icon-tasks"></i> Item Details</h3>
</div>
<div class="panel-body">
<%= form_for(#work, :html =>{:class =>"margin-bottom-40 new_item ",:multipart => true}) do |f| %>
<% if #work.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#work.errors.count, "error") %> found </h2>
<ul>
<% #work.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="form-group">
<%= f.label "Title",:class=>"control-label" %>
<div class="controls">
<%= f.text_field :title,:class=>"form-control",:placeholder=>"Title of the story/poem" %>
<div class="description">
<div class="">
<strong>Enter a suitable title for your story/poem
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :type,:class=>"control-label" %>
<div class="controls">
<%= f.select :type,{"Story"=>"Story","Poem"=>"Poem"},{},:class=>"form-control",:placeholder=>"Story/Poem" %>
<div class="description">
<div class="">
<strong>Is it a story or a poem?
</strong>
</div>
</div>
</div>
</div>
<%
visibilities=["Me Only","Anyone with link","Logged In Users","Everyone"]
%>
<div class="form-group">
<%= f.label :visibility,:class=>"control-label" %>
<div class="controls">
<% [ 3,2, 1, 0 ].each do |option| %>
<br><%= radio_button_tag :visiblity, option, #visibility == option %>
<%= label_tag "visibility_#{option}", visibilities[option].humanize %>
<% end %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :category_id,:class=>"control-label" %>
<div class="controls">
<%= f.collection_select :category_id,Category.all,:id,:name,{},:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
Type your story/Poem here
</strong>
</div>
</div>
</div>
</div>
<div class="form-group">
<%= f.label :content,:class=>"control-label" %>
<div class="controls">
<%= f.text_area :content,:class=>"form-control" %>
<div class="description">
<div class="">
<strong>
</strong>
</div>
</div>
</div>
</div>
<div class="actions">
<%= f.submit :class=>"btn btn-primary" %>
<%= link_to 'Back', works_path,:class=>"btn" %>
</div>
<% end %>
I have scaffolding for work with the parameter Type which accepts Story and Poem. When I submitted the form initially, I would get a stories_path not found error so I mapped stories and poems paths as follows:
resources :stories, :controller => 'works'
resources :poems, :controller => 'works'
So when I submit the first time, the form shows validation errors. When I submit it again after correcting validation, I get an exception
ActionController::ParameterMissing in WorksController#create
in this line:
params.require(:work).permit(:title, :type, :visibility, :category_id, :content)
I assume this is because its now renaming all the fields to story[] instead of work[]
How do I fix this and in the greater picture, how do I make STI work on the same controller. I just want to be able to use Poem.all and Story.all for fetching records.
Submisson currently works through the same interface
I am allowing the user to select the type of work in the form.

Resources