Rails3. Is there any way to separate javascript file for different controllers? - ruby-on-rails-3

I search around for quite some time but there's are no close solution for this.
For example,
I had generated 2 controllers: Articles, and Calendars.
There have been 2 javascript files also been generated according to these 2 controllers as:
calendars.js and articles.js.
The problem is, if they (2 files) all are included into application.js without any condition. i.e when I open articles/index action, the calendars.js has been imported on page
or when I open calendars/index action (or any action), the articles.js has been imported on page.
What I am looking for is a methodology to separate javascripts file according to the controller that they are belong to?
I also having the same question related to css files.
Thank you and Best regards,
Dat

What you probably want to do is only execute certain pieces of javascript on each controller (action). You can achieve that easily with this gem: https://github.com/intrica/rails_document_ready.

You can add in your index.html.erb file.
For the specific page where you want this js file add in your html file.
<%= javascript_include_tag "articles" %>

I found paloma: https://github.com/kbparagua/paloma
I develop new Rails application for about 2 months with this gem, and it provide quite effective way to manage javascript.

Related

Moving javascript files ROR 3.0 to 3.1

ok so I've got a couple of js files located in "views/relationships"….the original code is for 3.0…I'm on 3.1 and the javascript files are not working….how do I get these to works? here is the code for one of the files….
expounding on "not working":
the user clicks on a "unfollow" button, an ajax call takes place and runs on a function to do something, the "unfollow" button then changes to a "follow" button. That's what is supposed to happen...what actually...
UPDATE: turns out to be a simple prototype to jquery problem...
$("follow_form").update("<%= escape_javascript(render('users/unfollow'))%>")
$("followers").update('<%= "#{#user.followers.count} followers" %>')
becomes...
$("#follow_form").html("<%= escape_javascript(render('users/unfollow'))%>")
$("#followers").html('<%= "#{#user.followers.count} followers" %>')
Not all JavaScript files should be moved to app/assets/javascripts: only move files that are used to add functionality to your app (e.g. having tabbed menus with javascript). JavaScript templates rendering AJAX views (e.g. create.js.erb) should remain in the app/views/users/ folder.
So to answer your question: the files you're talking about (based on the 2 lines included in your question) should remain where they are.

ruby on rails 3, render multiple views in one view

I have a problem and I dont know how to solve it.
I have a user that log in a web site, and I identify them by session[:user_id] and user has a status page that is defined in user_controller and in status view.
So, I would like to make one page for admin, to see all the statuses from all users on one page, using already written controller and view.
Is that possible?
I could rewrite some of the code, so that I could call with params, like ?user_id=70 and then set session[:user_id]=params[:user_id], but it would be painful if I will have to rewrite whole statuses, beside i would have to maintain same code on 2 different places.
Thank you.
Dorijan
If you need more functionality in your controller, you can add more actions to it. You can also do that in a resourcefull way.
On the other hand it usually is best practice to keep controllers thin.
See: ActionController
In order to make your views reusable, you should use partials.
You could make a _user_status partial.html.erb and render a single partial for a user render all of them for an admin.
Checkout: Layouts and Rendering in Rails

How to use send_file in RoR

I'm very new to RoR and am trying to use send_file to have users download and view files that were uploaded by other users. I want to have a link on the project show page for each of the uploaded files. For now I am uploading the files using carrierwave and storing them in public/uploads/permit/avatar/permit_id/file. After going through much documentation I can't seem to figure out exactly what code to put into the model view and controller to get the links to show up on the show page. Can some one please help me with what code to put where.
if you want allow only authorithed users download files then you should set other path than public, something like:
private/uploads/permit/avatar/permit_id/file
and use something like send_file avatar.uploader_field.file in controller action then in views:
link_to "avatar", path_to_controller_action
For everyone:
link_to "avatar", avatar.uploader_field.url
or
link_to "avatar", avatar.uploader_field.path
in views

Rails 3 rendering iframe with raw()

So I am storing the whole embed code from youtube into my database and then outputting it like this.
<%= raw(people.video) %>
which outputs the general iframe tag copied from youtube:
<iframe src="foo" width=400 height=380></iframe>
the problem is that it actually displays that tag instead of embedding the video into the page itself. I can get around this just by storing the src in the database.... but this is part of a mini cms system and the site admins would find it much easier just to copy and past the embed code from youtube. Is there someway I can specify for the iframe to actually render instead of just spitting out the html on the page?
You can use the raw method to render the HTML of a string, are you storing it that way? You can also try the RedCloth gem - I was able to get this working like so:
<%= raw RedCloth.new(#page_content.content).to_html %>
Which allowed for my custom CMS to use the Textile markup
http://en.wikipedia.org/wiki/Textile_%28markup_language%29
I'm not 100% on what you're seeing, since raw should render HTML, but you might also try to include the embed tag using a JavaScript.write, since there are some issues related to Flash objects embedded in HTML vs JavaScript.
Let me know if this doesn't work, and if so, can you provide screenshots or copy what you're seeing on the page?

Rails : Can I use forms from the public/index.html file?

Am currently using Rails 3.0 to develop my app.
How to handle a form in the public/index.html file which I'm planning to use as my Home page.
Usually a view's action buttons gets paired with the corresponding method of its controller.
But how to handle this in case the index file in public directory?
This is possible, so long as the form's action attribute is pointing at an action of a Rails controller. However, it is not the norm in Rails to use a static HTML page to capture data. The norm is to use the MVC architecture of Rails.
I have to ask: Have you read the documentation provided on the Ruby on Rails website? Check out http://edgeguides.rubyonrails.org and read the Getting Started section.
Now, I think you might be using public/index.html because you don't know how to use a controller to serve the root of your website. Here's a quick example of how to use a controller instead of public/index.html:
Create a controller (i.e., home via rails g controller Home index)
Modify routes.rb and add root :to=>"home#index"
Delete public/index.html
The above is actually in the Edge Guides, Getting Stated section. So again, I strongly recommend you read the documentation. It will honestly save you a lot of trouble.