How to include code in only one controller action? - ruby-on-rails-3

I have only a single page that requires jquery ui in my entire application. How can conditionally include the javascript files in that single page?
I believe in Rails 2 I could use: (in application.html.erb)
<%- if controller.controller_name == "posts" && controller.controller_action == "new" -%>
<%= stylesheet_link_tag 'jquery-ui-1.8.13.custom.css' %>
<%= javascript_include_tag 'jquery-ui-1.8.13.custom.min.js', 'autocomplete-rails.js' %>
<%- end -%>
But controller.controller_action throws an undefined method error. And after looking at the API, it looks like it's been removed?
Maybe it would be best to remove the conditional from application.html.erb altogether and just put it at the top of posts/new.html.erb ?

I would avoid delegating responsibility for this to your application layout. If you don't need jQuery UI on more than a single view, you are best off letting the view handle that. The following let's you do just that while still keeping your output HTML clean and sensible (ie. not putting JS all over the place willy nilly).
In your layout (application.html.erb):
<head>
<title>Foo Bar</title>
<%= yield :page_specific_assets %>
</head>
In your view that requires jQuery UI (posts/new.html.erb):
<% content_for :page_specific_assets do %>
<%= stylesheet_link_tag 'jquery-ui-1.8.13.custom.css' %>
<%= javascript_include_tag 'jquery-ui-1.8.13.custom.min.js', 'autocomplete-rails.js' %>
<% end %>
Note: despite convention, putting unnecessary javascript in the <head> degrades performance.

I believe it's:
controller.action_name

Related

How to specify twitter bootstrap's <i> tag in Embedded Ruby

-Newbie to Ruby on Rails and integrating a few aspects of Twitter Bootstrap in (or at least trying :))-
I have a link in regular HTML with Twitter Bootstrap icons in it:
<li class="sidebar"><i class="icon-user"></i> Home</li>
(The i tag is for twitter's image icons)
If I were writing that same HTML in embedded Ruby, how could I still get the icon in there?
Right now I have:
<%= link_to content_tag(:li, "Home"), root_path %>
Is it possible to specify Twitter's tag in embedded ruby?
Also, should I specify the sidebar class in regular html or with :class => "sidebar"
Thanks!
Sure, just try something like:
<%= content_tag(:li, :class => 'sidebar') do %>
<%= link_to '#' do %>
<%= content_tag :i, '', :class => 'icon-user' %> Home
<% end -%>
<% end -%>
The shorter alternative I use is
link_to " ".html_safe, '#'
It's 'dirtier' but somewhat easier to understand.

Route not working in RoR

ok I am trying to invoke a DELETE HTTP command using embedded ruby.
SO my code is:
<li><%= link_to "Sign out", signout_path, method: "delete" %></li>
in my routes I got
#Note the use of via: :delete for the signout route, which indicated that it should be invoked using an HTTP DELETE request
match '/signout', to: 'sessions#destroy', via: :delete
but I get this error!
No route matches [GET] "/signout"
I wrote "method: "delete" !! so why does it give me a GET error? ?
guys applying what you told me, including application.js breaks my js code!!
Here is my head code:
</head>
<!-- Ruby Code -->
<%= stylesheet_link_tag "scaffold" %>
<%= stylesheet_link_tag "myCSS/home.css", :media => "all" %>
<%= stylesheet_link_tag "myCSS/JS_dropdown_menu.css", :media => "all" %>
<%= javascript_include_tag "myJS/jquery-1.7.js" %>
<%= javascript_include_tag "myJS/hoverIntent.js" %>
<%= javascript_include_tag "myJS/jquery.dropdown.js" %>
<%= javascript_include_tag "application.js" %>
<%= csrf_meta_tags %>
</head>
You're attempting to make a GET request to a route that would only respond to DELETE.
Why isn't this working? You probably haven't included application.js using this:
<%= javascript_include_tag :application %>
This would include the jquery.js and jquery_ujs.js files that would provide the method: "delete" functionality for your link.
The functionality of passing method: 'delete' relies on having a functioning UJS library. The first thing I would do is make sure that jquery and jquery_ujs JavaScript files are being included correctly.

tinymce issue with rails 3

I have legacy rails 3 app, where I need to modify a page to use tinymce to edit a text_area.
There are existing pages in this app that already use tinymce.
For reasons I cannot go into here, I cannot use any of the tinymce plugins that are available.
Now my problem is as follows.
I have a model called Sections, that has two attributes, section_name and html.
I want to be able to edit the html using tinymce.
My view has a form which is as follows
<%= form_for #section , :url => update_section_path , :method => :put do |f| %>
<%= f.hidden_field :id %>
<%= f.label :section_name , "Section Name" %>
<%= f.text_field :section_name %> <br />
<%= f.label :html, "Html" %>
<%= f.text_area :html %>
<%= f.submit "Update" %>
<% end %>
The form appears as expected.
The TinyMCE editor also appears on the page with the original html.
The problem is that when I click on the Update button, the put query sent to my server, does not contain the new modified content of the Html text_area. It sends back the original text that was in that text_area.
Could anyone help me understand why.
Thanks in advance
=Puneet
I found an issue in my html which was causing this. Once I fixed that issue it worked fine

RoR: form_for action button doesn't respond

I'm coding the project from Ruby on Rails Tutorial: Learn Rails by Example and am having trouble with the following and unfollowing features.
I have a piece of HTML in one of my pages that looks like this:
<%= form_for current_user.relationships.build(:followed_id => #user.id),
:remote => true do |f| %>
<div><%= f.hidden_field :followed_id %></div>
<div class="actions"><%= f.submit "Follow" %></div>
<% end %>
My RelationshipsController has a create method, but it is never called. The same applies to my 'unfollow' html and corresponding destroy method. Is there something I need to add to my project to let Rails know that the relationships.build method should call the create method, or is that automatic?
Thanks in advance.
RelationshipsController has a create method
current_user.relationships.build()
Is it create or build?
Show us the controller code. Since you use "remote=> true" you probably need to change the "respond_to" code in there and create a js.erb file.

Is it possible to use partials to be rendered as wrappers in Rails?

I would like to render structures like this:
<tag1>
<tag2 someattribute="somevalue">
<.. lot of things inside ..>
</tag2>
</tag1>
<tag1>
<tag2 someattribute="someothervalue">
<.. different inside things inside ..>
</tag2>
</tag1>
The tag1, tag2 are the same, they are just parametrized. The inner part of the code changes. I tried to implement the thing above like that (haml):
%div{id:['products', id]}
.products_content
%div{id:['products', id, 'content'], class:'products_mask'}
= yield
This was the partial _content_head.html.haml, which is called from a template:
= render 'shared/content_head', id: 'all' do
%h3= Title
%p= Body of the text.
My theory that yield inside the partial would lead to rendering of the passed block did not prove. Is there a way to use partials as code wrappers? Can you suggest me some solution how to reach this? Thank you.
This might be a good use of the capture method.
I'm only familiar with ERB, but here is the general idea:
<% structure = capture do %>
<h3>Title</h3>
<p>Body of text</p>
<% end %>
Then pass the variable into the partial:
<%= render 'shared/content_head', :structure => structure %>
And within the partial, spit out the structure variable:
<%= structure %>
Reset structure multiple times within the view as you render partials (or maybe more appropriately, in a helper?).
I've used the following (Rails 4, but I think it should work with Rails 3 too):
<%# app/views/users/_edit.html.erb %>
<%= render layout: 'modal_wrapping' do |f| %>
<%= f.input :email %>
...
<% end %>
.
<%# app/views/users/_modal_wrapping.html.erb %>
<div id='modal'>
<%= simple_form_for #user do |f| %>
<%= yield f %>
<% end %>
</div>