How to write this in ruby on rails? - ruby-on-rails

<div id="datetimepicker" class="input-append date">
<input type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
This is what I could come up with. And it was wrong!
<%= text_field_tag('datetimepicker',
content_tag(:span,
content_tag(:i,
{data-time-icon:'icon-time'},
{data-date-icon:'icon-calendar'}
),
{class: 'add-on'}
),
{class: 'input-append'}
)%>
Any help would be greatly appreciated.

text_field_tag is for input, for other you can use content_tag
This is your current markup:
<div id="datetimepicker" class="input-append date">
<input type="text"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
Using Rails helpers it can be modified to :
<%= content_tag :div, :class=> "input-append date", :id => 'datetimepicker' do %>
<%= text_field_tag '' %>
<%= content_tag :span, :class => "add-on" do %>
<%= content_tag :i, :data => {:time-icon => 'icon-time', :date-icon => 'icon-calendar'} %>
<% end %>
<% end %>
However I prefer Basic HTML over these ActionView Helpers, unless its helping out with code, which in this case is not much..

Related

Rails link_to issues

I am trying to make my rails application generate a link without the text and custom content instead. I am currently doing this
<%= link_to '', {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Which generates this code
<a action="show" controller="project" href>
<div class="image-container">
<img alt="House 2" src="/uploads/project_image/image/1/house-2.jpg" title="Ygfaweg">
</div>
<p>
<span class="title">
</span>
<span class="summary">
</span>
</p>
</a>
The link is incorrect and is missing alot of data, any idea why?
Remove '' from link_to method.
Change :controller in link_to method to projects
Remove braces from :id => project.id, :class => "projects"
So, refactor the first line to:
<%= link_to {:controller => :projects, :action => :show}, :id => project.id, :class => "projects" do %>
I didnt come to a pure conclusion with this issue. I ended up doing this which works fine.
<a class="project" href="<%= url_for({:controller => :project, :action => :show, :id => project.id}) %>">
Cheers everyone!
Try to do like this :
<%= link_to ({:controller => :project, :action => :show,:id => project.id}, {:class => "projects"}) do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>
Hope this will help you.
Just remove blank string from link_to and you are done.
<%= link_to {:controller => :project, :action => :show}, {:id => project.id, :class => "projects"} do %>
<div class="image-container">
<%= image_tag project.images[0].image.url, :title => project.title %>
</div>
<p>
<span class="title">
<%= project.title %>
</span>
<span class="summary">
<%= project.summary %>
</span>
</p>
<% end %>

How do I convert this form_tag helper into a simple_form_for call?

This is the HTML I am trying to produce:
<div class="col-lg-3">
<div class="input-group custom-search-form">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">
<span class="glyphicon glyphicon-search"></span>
</button>
</span>
</div><!-- /input-group -->
</div>
This is my vanilla Rails form helper:
<div class="col-lg-3">
<div class="input-group custom-search-form">
<%= form_tag(dashboard_index_path, :method => "get", id: "search-form", :class => "navbar-form navbar-left") do %>
<%= text_field_tag :search, params[:search], placeholder: "Search videos", :class => "form-control" %>
<span class="input-group-btn">
<%= submit_tag "", name: nil, class: "btn btn-default" %>
<span class="glyphicon glyphicon-search"></span>
</span>
<% end %>
</div><!-- /input-group -->
</div> <!-- //col-lg-3 -->
This is what this looks like:
Whereas what I am trying to achieve is this:
Generated from this:
http://bootsnipp.com/snippets/featured/search-box
I would like to generate the above HTML, using simple_form_for, rather than the form_tag helper.
So I have simple_form installed. How do I convert this form_tag into a simple_form_for call?
<%= form_tag(dashboard_index_path, :method => "get", id: "search-form", :class => "navbar-form navbar-left") do %>
You can do this with form_tag also. Try below code :
<%= form_tag(dashboard_index_path, :method => "get", id: "search-form", :class => "navbar-form navbar-left") do %>
<div class="col-lg-3">
<div class="input-group custom-search-form">
<%= text_field_tag :search, params[:search], placeholder: "Search videos", :class => "form-control" %>
<span class="input-group-btn">
<%= button_tag "", name: nil, class: "btn btn-default", type: "button" do %>
<span class="glyphicon glyphicon-search"></span>
<% end %>
</span>
</div><!-- /input-group -->
</div> <!-- //col-lg-3 -->
<% end %>
Form_tag do not generate an input html class and submit_tag neither a button html class, and maybe css code expect this class, try with button_tag

Customize Bootstrap Form In Rails View

I can't figure out the proper formatting to modify my form partial in my view to look like the bootstrap form below with a appended button like this;
<div class="col-lg-9">
<div class="input-group input-group-lg">
<input type="text" class="form-control">
<span class="input-group-btn">
<button class="btn btn-default" type="button">Go!</button>
</span>
</div><!-- /input-group -->
I would like the form to post to :home and the appended button to replace the default f.submit button in a Scaffold. Here's a Scaffold form partial that I'd like to modify.
<%= form_for #anesu, :html => { :class => 'form-horizontal' } do |f| %>
<div class="control-group">
<%= f.label :home, :class => 'control-label' %>
<div class="controls">
<%= f.text_field :home, :class => 'text_field' %>
</div>
</div>
<div class="form-actions">
<%= f.submit nil, :class => 'btn btn-primary' %>
<%= link_to t('.cancel', :default => t("helpers.links.cancel")),
anesus_path, :class => 'btn' %>
</div>
<% end %>
Please guide me on how to format my code and the right methods to use
<%= form_for #anesu do |f| %>
<div class="col-lg-9">
<div class="input-group input-group-lg">
<%= f.text_field :home, :class => 'form-control' %>
<span class="input-group-btn">
<%= f.submit "Go!", :class => 'btn btn-primary' %>
</span>
</div>
</div>
<% end %>

How to avoid html tags in edit page

I am using rails 4 and I have used jquery text-editor. For omitting html tags, I have used sanitize helper.it's working fine.But edit the form shows html tags in description field.
form.html.erb
<%= simple_form_for #newsletter, html: {class: 'form-inline form-horizontal'}, :validate => true do |f|%>
<br/></br/><br/>
<div class="tabable well">
<div class="tab-content">
<div class="tab-pane active">
<div class="inputs">
<h4 class="" style="border-bottom: 1px solid #dcdcdc;"> <img src="/assets/side_menu/news-and-events.png" alt="Home" ></i> Add NewsLetter</h4><br/><br/>
<div class="offset0">
<%= f.input :name %>
<%= f.input :description, as: 'text', :input_html =>{:rows => '20', :cols => '180', :class => 'wysiwyg input-block-level' }%>
</div>
</div>
<div class="form-actions">
<%= button_tag(type: 'submit', class: "btn btn-primary") do %>
<i class="icon-ok icon-white"></i> Save
<% end %>
<%= link_to new_newsletter_path, class: 'btn btn-inverse' do %><i class='icon-hand-left'> Cancel</i><% end %>
</div>
</div>
</div>
<% end %>
Edit Page
edit.html.erb
<%= render 'news/sidebar'%>
<%= link_to 'Back', newsletters_path, :class => 'btn btn-inverse animated rotateIn' %>
<%= link_to 'View', newsletter_path(#newsletter), :class => 'btn btn-success animated rotateIn pull-right' %>
<br><br>
<div class='row-fluid clear'>
<div class='box gradient'>
<div class=''>
<%= render 'form' %>
</div>
</div>
</div>
I don't know , how to use sanitize in edit page. Please Guide Me..
it appears you are missing a closing </div>

Rails3 - why my view loading times are so long?

So one week ago I started to move my old app written in Rails 2.3.5 to new 3.0. Unfortunately, I found out that some of the views are loading surprisingly slow.
Example:
Rails 2.3.5 Ruby 1.8.7 -> WEBrick: Completed in 297ms (View: 143, DB: 40)
Rails 3.0.0 Ruby 1.8.7 -> WEBrick: Completed in 3081ms (View: 261, ActiveRecord: 108)
Rails 3.0.0 Ruby 1.8.7 -> WEBrick: Completed in 2495ms (View: 356, ActiveRecord: 76), slightly improved version -> less SQL queries, more counting in the view
Rails 3.0.0 Ruby 1.9.2 -> WEBrick: Completed in 2941ms (View: 423, ActiveRecord: 90), imp. version
Times differ with every reload of course, but still you can get the idea - in Rails 3 this view loads 10 times slower, no matter if I use Ruby 1.8.7 or 1.9.2. I checked that in development and production environments as well as on Heroku - everytime it looks the same. Other views' loading times are about 2 times slower in 3.0.
Here you can find all the logs:
http://img811.imageshack.us/img811/8514/1rails235ruby187oldver.jpg
http://img217.imageshack.us/img217/5521/2rails300ruby187oldver.jpg
http://img7.imageshack.us/img7/3089/3rails300ruby187diffver.jpg
http://img534.imageshack.us/img534/5138/4rails300ruby192diffver.jpg
What's wrong here? I was busy for 1 week to adapt all my controllers, views, gems, plugins and configs to Rails3 and now I'm stuck... I feel I really should learn some PHP, because it would give me more control on what actually is happening.
The view goes here:
<%= render :partial => 'menu' %>
<div id="preload">
<%= image_tag("/images/next_big.png") %>
</div>
<div class="clearer"><hr /></div>
<div class="picture">
<%= link_to image_tag(#picture.img.url(:page), :alt => #picture.title.gsub("; "," "), :border => 0), :action => "fullsize", :link => #picture.link %>
</div>
<div class="right">
<div class="title"><%= raw #picture.title.gsub("; ","<br />") %></div>
<div class="tags">
<% taglist = #picture.cached_tag_list.split(", ") %>
<% x = taglist.length %>
<% y = 0 %>
<% x.times do %>
<%= taglist[y] %><% y += 1 %><% if y != x %>,<% end %>
<% end %>
</div>
<div class="datetime">Posted <%= time_ago_in_words(#picture.created_at) %> ago</div>
<% this_voting = "voting" %>
<div id="<%= this_voting %>" class="voting">
<%= link_to image_tag("/images/yes.png", :alt => "Vote up", :class => 'vote_button'), {:action => :yes_vote, :id => #picture.id}, :remote => true %>
<span id="<%= this_voting + '_result' %>"><%= #picture.voting %></span>
<%= link_to image_tag("/images/no.png", :alt => "Vote against", :class => 'vote_button'), {:action => :no_vote, :id => #picture.id}, :remote => true %>
Pageviews: <%= #picture.views %>
</div>
<div class="link">Link:<br /><input type="text" value="http://bzzzzzzzzzzz.com/<%= #picture.link %>" /></div>
<div class="share">Share:<br />
<a href="http://facebook.com/sharer.php?u=http://bzzzzzzzzzzz.com/<%= #picture.link %>" target="_blank">
<img src="/images/mfb.png" alt="Facebook" onmouseover="puttext(fb, 0)" onmouseout="cleartext(0)" border="0" /></a>
<a href="http://twitter.com/home?status=http://bzzzzzzzzzzz.com/<%= #picture.link %>" target="_blank">
<img src="/images/mtwitter.png" alt="Twitter" onmouseover="puttext(twitter, 0)" onmouseout="cleartext(0)" border="0" /></a>
<a href="http://digg.com/submit?url=http://bzzzzzzzzzzz.com/<%= #picture.link %>&title=<%= #picture.title.gsub("; "," ") %>&media=image&topic=comedy" target="_blank">
<img src="/images/mdigg.png" alt="Digg" onmouseover="puttext(digg, 0)" onmouseout="cleartext(0)" border="0" /></a>
<a href="http://reddit.com/submit?url=http://bzzzzzzzzzzz.com/<%= #picture.link %>&title=<%= #picture.title.gsub("; "," ") %>" target="_blank">
<img src="/images/mreddit.png" alt="Reddit" onmouseover="puttext(reddit, 0)" onmouseout="cleartext(0)" border="0" /></a>
<a href="http://stumbleupon.com/submit?url=http://bzzzzzzzzzzz.com/<%= #picture.link %>" target="_blank">
<img src="/images/msu.png" alt="StumbleUpon" onmouseover="puttext(su, 0)" onmouseout="cleartext(0)" border="0" /></a>
</div>
<div id="share_caption0" class="share_caption"></div>
<div class="small_clearer"></div>
<% if #picture.last_comment_body %>
<div class="links_to_c">
<%= #picture.comments_count %> comment<% if #picture.comments_count > 1 %>s<% end %>
| <%= link_to "Write a comment", :anchor => "add_comment" %>
</div>
<% else %>
<div class="links_to_c">
<%= link_to "No comments, write a comment", :anchor => "add_comment" %>
</div>
<% end %>
<% if #picture.fullsize == true %>
<div class="fullsize">
<%= link_to "Fullsize", :action => "fullsize", :link => #picture.link %>
</div>
<% end %>
</div>
<div class="next">
<% if #picture.id != 1 %>
<% next_pict_link = Picture.find(#picture.id.to_i - 1).link %>
<%= link_to image_tag("/images/next.png", :alt => 'Next picture', :border=>0, :onmouseover => "this.src='/images/next_big.png'", :onmouseout => "this.src='/images/next.png'"), :action => 'show', :link => next_pict_link %>
<% else %>
<% last_pict_link = Picture.find(:last).link %>
<%= link_to image_tag("/images/next.png", :alt => 'Latest picture', :border=>0, :onmouseover => "this.src='/images/next_big.png'", :onmouseout => "this.src='/images/next.png'"), :action => 'show', :link => last_pict_link %>
<% end %>
</div>
<div class="clearer" style="padding-bottom: 0px"><hr /></div>
<h1><%= #thumbnails_text %></h1>
<% #pictures.each do |pict| %>
<%= link_to image_tag(pict.img.url(:thumb), :alt => "", :border => 1, :class => 'thumbnail'), :action => "show", :link => pict.link %>
<% end %>
<div class="clearer" style="padding-top: 6px; padding-bottom: 15px"><hr /></div>
<% if #picture.last_comment_body != nil %>
<a name="comments"><h1>Comments:</h1></a>
<% #picture.comments.each do |comment| %>
<div id="<%= comment.id %>" class="comment">
<font class="c_author"><%= comment.author %></font>
<font class="c_datetime"><%= time_ago_in_words(comment.created_at) %> ago</font>
<font class="c_id">#<%= comment.id %></font><br />
<div class="c_body"><%= comment.body.gsub(/\n/, '<br />') %></div>
</div>
<% end %>
<% end %>
<div id="insert_here"></div>
<a name="add_comment"></a>
<%= form_for [#picture, Comment.new], :remote => true do |f| %>
<div id="add_comment" <% if #picture.last_comment_body == nil %>style="margin-top: 15px"<% end %>>
<div class="f_section">
<div class="f_type" style="padding-right: 46px"><%= f.label :author, "Nick:" %></div>
<div class="f_field"><%= f.text_field :author, :maxlength => 40 %></div>
</div>
<div class="f_section">
<div class="f_type"><%= f.label :body, "Comment:" %></div>
<div class="f_field"><%= f.text_area :body, :maxlength => 2000, :rows => 6 %></div>
</div>
<div class="f_section">
<div class="f_type" style="padding-right: 20px"><%= f.label :captcha, "Captcha:" %></div>
<div class="f_captcha"><%= raw recaptcha_tags %></div>
</div>
<div class="f_submit">
<%= f.submit "Submit", :class => 'submit' %>
</div>
</div>
<% end %>
<div class="clearer bottom_clearer fifty_from_top"><hr /></div>
There's a lot that can go wrong in any environment, especially PHP.
What could be the issue here is using WEBrick with Rails 3. Using an alternative like mongrel or Passenger could help narrow down this problem.
You could have a situation where something that should be working is timing out, for instance, Memcache is not configured correctly, or a CURL request is being made that eventually fails.
It's always handy to have a very basic view with nothing in it you can use to benchmark the baseline performance of your application. A simple controller with a single action and an empty view can do the trick here. Diagnosing the problem will be a case of adding some of your controller code to this empty action until you can trigger the problem, or stripping parts out of your views to see if that's what causes it.
I usually start commenting out partials to see if one of them is causing the drag, and from there move back to the controller to pay close attention to what's getting loaded.
So.. I think I have tracked down the problem. I saw that WEBrick is giving me back two deprecation warnings (related to RAILS_ROOT and RAILS_ENV). I didnt use any of these phrases by myself, so I started to look for them in my plugins and gems. I found out that they are being used by paperclip (2.3.4) and recaptcha gem (recaptcha by ambethia 0.2.3). Then I removed paperclip from my Gemfile and commented all paperclip related settings from my model. The loading times dropped to 400-500ms.
So I guess I need to wait until somebody will fix paperclip... Im a ruby-newbie, so I wont be able to do this by myself :/

Resources