I am working on a rails app, which is getting upgraded from rails(3.0.9) to rails (3.2.22). I will try to demonstrate the problem through a demo rails app.
I created demo app's for different rails versions. I have something like this inside.
class ApplicationController < ActionController::Base
protect_from_forgery
layout :select_layout
def select_layout
#tmp = 'tmp variable'
#layout = 'application'
end
end
And accessing #tmp inside layouts/application.html.erb :-
<!DOCTYPE html>
<html>
<head>
<title>TwistageScopeIssue</title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %> <br/>
<%= #tmp %> <br/>
</body>
</html>
Values(#tmp) displays correctly('tmp variable') in : 3.0.x and 3.1.x .
Values(#tmp) gets set to nil : 3.2.x onwards
I am still looking into the rails source code to get some information, meanwhile any thoughts/idea's would be really helpful.
Thanks
Related
I'm trying to generate dynamic content in my views.
in my application.html.haml
!!!
%html
%head
%title YieldUsage
= stylesheet_link_tag "application", :media => "all"
= javascript_include_tag "application"
= csrf_meta_tags
%body
= yield
= yield :head
= yield :scripts
= yield :name_section
and my helper is like;
def name_section_form
content_for :name_section do
label_tag(#post.name)
end
end
and my view;
%article
= name_section_form
But it renders;
<body>
<article>
</article>
<label for="asfd">Asfd</label>
</body>
Why is it happening? Bug or something? Any ideas?
Thanks.
Çağdaş.
Your view gets dumped into the yield. The method sets the content for a specific yield. Remove the content_for in your helper.
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.
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
I have a problem very similar to this one: rails 3 - link_to to destroy not working
But delete/destroy links do not work; I simply get redirected to the show page of the object. When I make a button for delete, it all works fine. But I'd like to understand why. Does anyone know?
I seems to be related to some .js files I am using/calling.
<!-- This link doesn't work -->
<%= link_to('Delete', post, :confirm => 'Are you sure?', :method => :delete) %>-->
<!-- This button does work -->
<%= button_to "delete", post, :method=>:delete, :class=>:destroy, :confirm => 'Are you sure?' %>
Post Controller
def destroy
#post = Post.find(params[:id])
#post.destroy
respond_to do |format|
format.html { redirect_to(posts_url) }
format.xml { head :ok }
end
end
UPDATE
After doing some further research it seem that everyone else having a similiar issue has included the following jquery library:
<script type="text/javascript" src="/javascripts/dragdrop.js"></script>
But I still don't know what the issue is...
LOG
Started GET "/posts/7" for 127.0.0.1 at Tue Jul 12 08:34:06 -0400 2011
Processing by PostsController#show as HTML
Parameters: {"id"=>"7"}
Post Load (0.2ms) SELECT "posts".* FROM "posts" WHERE "posts"."id" = 7 LIMIT 1
SQL (0.2ms) SELECT COUNT(*) FROM "comments" WHERE ("comments".post_id = 7)
Rendered posts/show.html.erb within layouts/application (116.5ms)
HTML generated
Delete
UPDATE: I've found that removing <script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script> fixes my problem. But I don't understand why. Is there a known conflict between jquery1.5.1 and rails 3.0.7?
Make sure you include these in your application layout:
<%= javascript_include_tag(:defaults) %>
<%= csrf_meta_tag %>
In my case adding
<%= javascript_include_tag(:defaults) %>
did not work. However explicitly defining java script files did the trick
<%= javascript_include_tag 'prototype' %>
<%= javascript_include_tag 'application' %>
Not sure yet why :defaults tag didn't work...
Rails will automatically load jquery for you if you have jquery-rails gem loaded via your Gemfile (this is the default rails configuration). It is then loaded via:
<%= javascript_include_tag(:defaults) %>
Have a look at app/assets/javascripts/application.js for the code that tells rails to add jquery via assets:
//= require jquery
//= require jquery_ujs
By trying to load another copy of jquery via this line:
<script type="text/javascript" src="/javascripts/jquery-1.5.1.min.js"></script>
You have caused a clash of jquery instances, the effect of which will be that neither will work.
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