Make my own link_to in Rails - ruby-on-rails-4

I'm a new Rails developer and this my first question here so please be gentle.
I've been using Twitter Bootstrap in my application to put tooltips and Glyphicons on buttons on my CRUD links as follows:
Show:
<%= link_to master_group, class: "btn btn-default", data: {toggle: "tooltip", placement: "left"}, title: "Show" do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
Edit:
<%= link_to edit_master_group_path(master_group), class: "btn btn-default", data: {toggle: "tooltip", placement: "left"}, title: "Edit" do %>
<span class="glyphicon glyphicon-pencil"></span>
<% end %>
Destroy:
<%= link_to master_group, method: :delete, data: { confirm: 'Are you sure?',toggle: "tooltip", placement: "left" }, class: "btn btn-default", title: "Delete" do %>
<span class="glyphicon glyphicon-trash"></span>
<% end %>
Would it be possible make my own versions of link_to, so I could just change the path in each call, for example:
link_to_show master_group
link_to_edit edit_master_group_path(master_group)
link_to_destroy master_group
I was trying to follow the Rails DRY principles and found myself repeating this code for each view. I was also conscious that if for example, I wanted to change the tooltip, I'd have to do it on every single view. It would also allow me to be certain that my application had good uniformity.
Could anyone help me please.
Thanks in advance.

Create your custom link_to(s) in ApplicationHelper
Example
module ApplicationHelper
def link_to_show(path)
link_to path, class: "btn btn-default", data: {toggle: "tooltip", placement: "left"}, title: "Show" do
'<span class="glyphicon glyphicon-search"></span>'.html_safe
end
end
end

Related

Showing an array of messages in my blog app

I am building an blogapp and allowed users to comment to the post with their name and if the user is not signed in it will show as anonamys. Its working but in my browser it is showing an array full of comments. How do i get rid of that. In my Comments Controller
def create
params.permit!
#post = Post.find(params[:post_id])
#comment.user_id = current_user.id if current_user || nil
#comment.save
redirect_to post_path(#post)
end
In the form partial of comments
<%= form_for [#post, #post.comments.build] do |f| %>
In my Posts Controller Show Action
<h4><%= #post.comments.count %> Comments</h4>
<%= render partial: "comments/comment" %>
<%= render partial: "comments/form" %>
And in my Comments Partial I have
<%= #post.comments.each do |comment| %>
<p class="comment_name"><strong><%= (comment.user.user_name if comment.user) || "Anonymous" %></strong></p>
<p class="comment_body"><%= comment.body %></p>
<p class="comment_time"><%= time_ago_in_words(comment.created_at) %> Ago</p>
The array message in my browser is showing as
[#<Comment id: 13, name: nil, body: "nice pic", post_id: 17, created_at: "2015-09-27 20:54:22", updated_at: "2015-09-27 20:54:22", user_id: 8>, #<Comment id: 14, name: "sss", body: "sssss", post_id: 17, created_at: "2015-09-27 21:41:13", updated_at: "2015-09-27 21:41:13", user_id: nil>]
I have no clue where the mistake occurs. Thanks in advance.
The problem is in this line
<%= #post.comments.each do |comment| %>
which should be
<% #post.comments.each do |comment| %>
Notice the change <%= %> to <% %>.
<% %> - execute the statement.
<%= %> - prints the output.

Adding space between labels and fields

In my Rails app, the labels for the fields on the login page are way too close to the fields themselves, making it look cramped. I want to add space between them, but am not sure how.
I have Rails 4 with simple_form, bootstrap 3, and devise installed.
This is my app/views/devise/sessions/new.html.erb code:
<div class="row">
<div class="col-xs-4">
<%= simple_form_for(resource, as: resource_name, url: session_path(resource_name), html: {class: "well"}) do |f| %>
<fieldset>
<legend>Log in</legend>
<%= f.input :email %>
<%= f.input :password %>
<% if devise_mapping.rememberable? -%>
<div class="field">
<%= f.input :remember_me, as: :boolean %>
</div>
</fieldset>
<% end -%>
<div class="actions">
<%= f.button :submit, "Log in" %>
</div>
<% end %>
<%= render "devise/shared/links" %>
</div>
<div class="col-xs-8">
<h2>Signing in is easy and secure</h2>
</div>
</div>
And here is the entire github repo: https://github.com/yamilethmedina/wheels_registration
I've been looking in my bootstrap.css file and can't find relevant classes. What do you think I should do next?
You could add your own label with space after the custom label.
<%= f.input :email, label: 'Email ' %>
<%= f.input :password, label: 'Password ' %>
Or you could use tools like chrome dev tools to inspect the label element to see what class the labels are given and then add some custom css. example .label-class { margin-right: 10px; }

rails button_to with url helper not using index/get

I have a button_to and trying to use it with bootstrap3 buttons so the code is as thus:
<%= button_to(groups_url, {method: :get} , :class => 'btn btn-default btn-lg') do %>
<span class="glyphicon glyphicon-user" aria-hidden="true"></span>Group
<% end %>
I have another button here:
<%= button_to(providers_url, :class => 'btn btn-default btn-lg') do %>
<span class="glyphicon glyphicon-user" aria-hidden="true"></span>Provider
<% end %>
Notice the difference the first one works with the {method: :get} if i take that out I get the following error cliking on the button:
param is missing or the value is empty: group
In:
def group_params
params.require(:group).permit(:name, :address, :city, :state, :groupzip, :phone, :fax, :npi, :contact, :tin, :startdate, :enddate, :changedate, :change, :notes, :billingid, :mailingaddress, :mailingcity, :mailingstate, :termedcomment, :istermed, :mailingzip)
end
I am not sure why its not getting the method get, i mean it is a index route between a post and a get. Is the do call doing something funky?
Also if I do add the {method: :get} to make clicking on the button NOT give me the error, the button then looks different?! So not sure what to do here to get it to look and work right?

simple_form inline on rails 4

I am using a simple_form and bootstrap in rails 4. I want the start date and start time to be on the same line and end date and end time on the same line. I have tried the solutions from similar problems but none of them are working. I am a newbie in rails. Please help me.
Here is my code:
<%= b.input :start_date, wrapper: :horizontal_input_group do %>
<%= b.input_field :start_date, class: "form-control date start" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<% end %>
<%= b.input :start_time, wrapper: :horizontal_input_group do %>
<%= b.input_field :start_time, as: :string, class: "form-control time start" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
<% end %>
<%= b.input :end_date, wrapper: :horizontal_input_group do %>
<%= b.input_field :end_date, class: "form-control date end" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
<% end %>
<%= b.input :end_time, wrapper: :horizontal_input_group do %>
<%= b.input_field :end_time, as: :string, class: "form-control time end" %>
<span class="input-group-addon"><span class="glyphicon glyphicon-time"></span></span>
<% end %>
<% end %>
getbootstrap.com/css/#grid this is what you should read. After wrapping your code with a <div class="container"></div> and inside that a <div class="row"></div> you can use the bootstrap columns to make your way and align everything perfectly.

Backbone, stackmob sub-models - render to html

I am really new to backbone so any help on this would be awesome, even just to point me in the direction of some resources related to this.
I have tried to create a fiddle but it won't work the same as on my machine: http://jsfiddle.net/Wh2H5/
Basically what I need to do is render the array of objects(see the image below) as parts of the template.
This is the view I am rendering:
var ListView = Backbone.View.extend({
tagName: 'ul',
className : 'nav nav-list',
initialize: function() {
this.collection.bind('all', this.render,this);
this.template = _.template($('#item-list').html());
},
render:function (eventName) {
$(".bike_list ul").empty();
this.collection.each(function(bike){
this.$el.append(this.template(bike.toJSON()));
},this);
return this;
}
});
So to see the problem copy the code from this fiddle and paste it to a html document.
I can highly recommend Marionette on top of Backbone to save you a lot of boiler plate for managing Views.
You can in-line javascript in your template and loop through the part_rel array and render the objects accordingly. The code would look like:
<% _.each(part_rel,function(part) { %>
<li>
Part Name: <%= part.name %>
After Hp: <%= part.after_hp %>
</li>
<% }); %>
Here is the code added to your template:
<script type="text/template" id="item-list">
<li class="<%= model %>">
<strong>Bike ID:</strong> <%= bikes_id %><br>
<strong>Model:</strong> <%= model %><br>
<strong>Before cc:</strong> <%= before_cc %><br>
<strong>Before ci:</strong> <%= before_ci %><br>
<strong>Before HP:</strong> <%= before_hp %><br>
<strong>Before torque:</strong><%= before_torque %><br>
<strong>Related parts:</strong><ol>
<% _.each(part_rel,function(part) { %>
<li>
Part Name: <%= part.name %>
After Hp: <%= part.after_hp %>
</li>
<% }); %>
</ol>
<br><br>
</li>
</script>
Read up on underscore's documentation for template. Also, here is a working example, http://jsfiddle.net/mFwsK/.