How to edit rails generator views - ruby-on-rails

I am using rails 4.2.6 and I wanted to edit default generator views. A generator which is:
Rails g controller index about
On this generator I am getting views but I want to edit that default views for that I am using this folder structure under:
#lib/templates/erb/controller/view.html.erb
<h1><%= class_name %>#<%= #action %></h1>
<p>Find me in <%= #path %></p>
For scaffold editing works perfectly when I used this: lib/templates/erb/scaffold/index.html.erb
I just want to know how to overwrite this controller views. My solution is not working. And thanks in advance.

Related

Rails Modify Scaffold Form

Using rails 5.0.0 for building a simple timecard module for a web application. My question is how do I go about modifying a form which is automatically being rendered by rails scaffold
rails g scaffold Timesheet user:string clock:string time:datetime
=== new.html.erb file ===
<%= render 'form', timesheet: #timesheet %>
In the user string want to add <%= current_user.email %> and in clock string want to add a drop-down? What is the best way to do this? Already have database table in place etc...
Illustration below
Thanks in advance!
The form is in an automatically generated partial, located in app/views/timesheets/_form.html.erb.
Ok, in: app/views/timesheets - you should find your form. If you change something in form, check it with your 'new' merhod (or with last method - if you didn't change/add anything: this method will get your params) in controller.
When you run
$rails generate scaffold <name>
you will auto-generate a ready to use controller, model, and views with a full CRUD (Create, Read, Update, Delete) web interface.
You can modify each of the CRUD operations to your liking, in your case timesheets view which is located in app/views/timesheets/.

Adding custom javascript to device

In my javascripts folder for my rails application, I've added both a device.js file as well as a device/registrations.js file. I was under the impression that when I'm routed to the registration page, rails would automatically pick up the correct javascript files, but for some reason it's not working.
Is there anything that needs to be added so that it can use custom javascript code?
You can include that in your registration view of devise by doing:
<%= javascript_include_tag 'devise/registrations' %>
To generate the views do
rails g devise:views
EDIT
Another way would be in the views/devise folder create a partial _registrations.html.erb
Put your js code in there and then do
<%= render 'registrations' %>

Need to exclude pre and next buttons from kaminari pagination

I want to change kaminari pagination paginate helper format like
First,Prev - current page - Next,Last
Because its breaking my design with unwanted numbers.
I tried with all other helpers but no success.
Is there any way? Please let me know your thoughts.
Here's my thoughts. You need to go to the Kaminari GitHub page. That link leads to section about generating Kaminari partials, so you can edit them just like you want. Right from that page:
rails g kaminari:views default -e haml
Where haml is your template engine. You can replace it by erb, slim (depending on what you prefer to use).
Update. Here's a related question about customizing Kaminari templates.
Use Kaminari's themes
<%= paginate #users, :theme => 'my_custom_theme' %>
you need custom kaminari view files in
app/views/kaminari/my_custom_theme
And another simpler solution, what happen if you do this?
<%= paginate #users, :window => 0 %>
You can customize it a little more using CSS, each part of the pagination widget has some class or id, you can hide/show/modify them
[vidur#centos7-demo tukaweb]$ rails g kaminari:views default -e erb
Running via Spring preloader in process 25960
create app/views/kaminari/_first_page.html.erb
create app/views/kaminari/_gap.html.erb
create app/views/kaminari/_last_page.html.erb
create app/views/kaminari/_next_page.html.erb
create app/views/kaminari/_page.html.erb
create app/views/kaminari/_paginator.html.erb
create app/views/kaminari/_prev_page.html.erb
in the view:
<%= paginate #data %>

Submit form without using object

It's my first post on stackoverflow! I warn you, I just started learning Ruby on Rails.
Currently, I'm working on a music scanning project in rails. I'm trying to process al the music on the hard disk. To do that, the user can add a folder to scan when he is inside the config part of the app. So I store inside a YAML file all the config of my app.
I create a 'config' controller, that contains 2 view for the moment: "adddir" and "index".
The index view show all parameters, that have been gathered by loading the YAML config file (done by the controller 'config').
Next, I want to give the possibility to the user to add a new folder to scan. So I did a static route to the 'adddir' view:
routes.rb file:
match "/config/adddir" => "config#adddir"
Now, I want to create a form that get the path provided by the user, and add it to the YAML file.
So my question is: How to create a form that be able to use the method config#addfolder I created when the user click on submit button?
I hope this is enough clear.
Best regards
Welcome to SO. Its a very basic question. Just have a look at the screencast. Make a simple project first (with a model), then you will see the ABC of Rails.
http://rubyonrails.org/screencasts/rails3/
Basically:
You generate the controller like:
rails g controller config adddir
This setups the controller, the view and your route.
Then go into your view folder and look inside config. There is adddir.html.erb. This file you need to change in something like a form.
You can open your form:
http://localhost:3000/config/adddir
rake routes
Will print you a list of available routes you can use (inside your form).
Now the magic is that Rails generate your a config_adddir_path (watch the _path). That you can use to hookup the form.
<%= form_tag(config_adddir_path, :method => "get") do %>
<%= label_tag(:q, "Folder:") %>
<%= text_field_tag(:q) %>
<%= submit_tag("index") %>
<% end %>
Inside your controller (def adddir):
You can access the parameters:
def adddir
logger.debug params.inspect
end
Have a look here:
http://guides.rubyonrails.org/form_helpers.html

How to create a website form field in Rails 3?

I have a form where I'd like to be able to input a web address, like "google.com", into a form field, and be able to click on this link in the show view of the submitted form.
How can I accomplish this?
show.html.erb
<p>
<strong>Website:</strong>
<%= link_to #video.website, #video.website %>
</p>
Scaffolding will handle it like Jason Swett said. If you are looking to put it in a link in show just do something like this:
<%=link_to #link.name, "http://"+#link.url%>
If that doesn't work you could always do:
<%=#link.name%>
Scaffolding will handle this for you. I recommend going through the Rails Guides, especially this one: http://guides.rubyonrails.org/generators.html
And more specifically, you can generate a model with an attribute called url like this:
rails generate scaffold Thing url:string
And then run your migrations:
rake db:migrate
You should end up with some views in app/views/thing/ and a controller at app/controllers/thing_controller.rb. That should put you on your way.
If you have a model that has an attribute like web_address, you can do:
= text_field_tag, "web_address", ""
(using HAML syntax and tags for form_tag)
And then in your show you can do:
= link_to model.web_address, model.web_address
Which will make a link like: http://www.google.ca

Resources