Pass local rails variable to javascript to partial - ruby-on-rails-3

I'm giving up my search, I normally try to figure these things out on my own but I'm struggling hard and I just want this to work
I have the link_to seen below where #puser is the user of the profile I'm currently viewing.
<%= link_to 'Request', new_or_edit_relationship_path(nil), :remote => true, :locals => { :puser => #puser} %>
This in turn calls new_relationship_path which is a .js.erb file seen below
alert("<%= escape_javascript(puser.id) %>")
Why won't this work!? It's saying the puser variable or method is undefined. This works perfect if I was to just render a partial passing in the locals but no. Javascript doesn't want to play nice
Could anyone help explain why me or the program is stupid?

When you do a link_to as remote, the user is starting an entirely new request when they click the link. So passing a local means nothing to the new request. (The local doesn't exist any more on the new request.)
So in order for the #puser to exist on the new request, you need to pass the id for that #puser via the URL (whatever you have going on for new_or_edit_relationship_path). The new request needs to look up the puser by that id, and then it can use it in the JS alert().
Hope that helps and is a little clearer than mud.

Related

Rails 3 form actions and methods

I have made a resource.
resources :dashboards
I have a partial file which contains a form and I want to use this partial (as the form elements won't change) to update and create. So here is what I have:
Controller
class DashboardsController < ApplicationController
def new
#dashboard = Dashboard.new
end
end
View
/dashboards/new.html.erb
<%= render :partial => "form", :locals => { :dashboard => #dashboard } %>
Partial Form
/dashboards/_form.html.erb
<%= form_for(#dashboard) do |form| %>
.....
<% end %>
Ruby Guide
The Ruby Guide states:
The Article model is directly available to users of the application, so — following the best practices for developing with Rails — you should declare it a resource. When dealing with RESTful resources, calls to form_for can get significantly easier if you rely on record identification. In short, you can just pass the model instance and have Rails figure out model name and the rest. For example:
## Creating a new article
# long-style:
form_for(#article, :url => articles_path)
# same thing, short-style (record identification gets used):
form_for(#article)
## Editing an existing article
# long-style:
form_for(#article, :url => article_path(#article), :html => { :method => "put" })
# short-style:
form_for(#article)
Result
I thought I have followed the Rails Guide correctly. Because I made #dashboard a resource. I could just pass it into the form and have it handle the action, method and the rest. Instead I'm getting this:
<form accept-charset="UTF-8" action="/dashboards" class="new_dashboard" id="new_dashboard_" method="post">
According to the docs. Shouldn't the action of my form now be "/dashboards/new" because we are on the new action? And should it be passing an extra field declaring the method to be put when I use the same code in the /edit action??
My result is always the same no matter what. The form never changes.
What am I doing wrong?
EDIT
Here is my router info from rake routes
GET /dashboards(.:format) dashboards#index
POST /dashboards(.:format) dashboards#create
GET /dashboards/new(.:format) dashboards#new
GET /dashboards/:id/edit(.:format) dashboards#edit
GET /dashboards/:id(.:format) dashboards#show
PUT /dashboards/:id(.:format) dashboards#update
DELETE /dashboards/:id(.:format) dashboards#destroy
You are correct that you should be able to "pass #dashboard into the form and have it handle the action, method and the rest." The issue here is what new is in the context of RESTful actions.
When you declare a set of resources with resources :dashboards, you are creating a set of routes which map requests to controller actions:
GET /dashboards index
GET /dashboards/new new
POST /dashboards create
GET /dashboards/:id show
GET /dashboards/:id/edit edit
PUT /dashboards/:id update
DELETE /dashboards/:id destroy
You can check this if you run rake routes.
The issue here is that the new action is defined as a GET request to the path /dashboards/new, i.e. this is the route for the form itself. The URL in the action attribute of the actual form is something else: this is where the form will post the data to with a POST request, which on the server (rails) side will map to the create controller action.
When you use the form helper with form_for(dashboard), a form is created with a route corresponding to what dashboard is: if it is a new record (i.e. it does not yet exist in the database), then the form action will be create (and point to /dashboards), whereas if it already exists it will point to the actual URL for the record (e.g. /dashboards/123). This is what makes the form helpers so useful.
So, to sum up, /dashboards is the correct URL, not for the new action but for the create action, which the form helper uses because dashboard is a new record. new is the route to the page where the form resides, i.e. /dashboards/new.
Hope that makes sense.
p.s. as a side note, you shouldn't be accessing #dashboard in the partial if you are passing it in as a local (:locals => { :dashboard => #dashboard }). Just use dashboard.

Rails _path helper generating path with format not id

In my routes.rb I have:
resources :aquariums do
resources :management_roles
resources :graphs
resources :animals
end
get 'aquarium', to: 'aquariums#show', :as => :aquarium
The reason for the last get is I have the notion of "current aquarium" in my app. If, say, current_aquarium is set to 1, then in my controller's 'show' action '/aquarium' gets the same rendering as '/aquariums/1' with code like
#aquarium_id = params[:id] || current_aquarium.id
Now, and I'm assuming this is thanks to this different routing, this code:
<%= link_to aquarium.name, aquarium %>
or
<%= link_to aquarium.name, aquarium_path(aquarium) %>
Generates paths like this:
/aquarium.1
where I'd typically expect:
/aquariums/1
Right?
Per request... here's what rake routes | grep aquar yields:
(I'm assuming it's that very last route that is messing things up, but I would have thought that it would process these in order. And, just FYI, I originally had that route at the top. Moved it to the bottom assuming it would fix).
aquarium_management_roles GET /aquariums/:aquarium_id/management_roles(.:format) management_roles#index
POST /aquariums/:aquarium_id/management_roles(.:format) management_roles#create
new_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/new(.:format) management_roles#new
edit_aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id/edit(.:format) management_roles#edit
aquarium_management_role GET /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#show
PUT /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#update
DELETE /aquariums/:aquarium_id/management_roles/:id(.:format) management_roles#destroy
aquarium_graphs GET /aquariums/:aquarium_id/graphs(.:format) graphs#index
POST /aquariums/:aquarium_id/graphs(.:format) graphs#create
new_aquarium_graph GET /aquariums/:aquarium_id/graphs/new(.:format) graphs#new
edit_aquarium_graph GET /aquariums/:aquarium_id/graphs/:id/edit(.:format) graphs#edit
aquarium_graph GET /aquariums/:aquarium_id/graphs/:id(.:format) graphs#show
PUT /aquariums/:aquarium_id/graphs/:id(.:format) graphs#update
DELETE /aquariums/:aquarium_id/graphs/:id(.:format) graphs#destroy
aquarium_animals GET /aquariums/:aquarium_id/animals(.:format) animals#index
POST /aquariums/:aquarium_id/animals(.:format) animals#create
new_aquarium_animal GET /aquariums/:aquarium_id/animals/new(.:format) animals#new
edit_aquarium_animal GET /aquariums/:aquarium_id/animals/:id/edit(.:format) animals#edit
aquarium_animal GET /aquariums/:aquarium_id/animals/:id(.:format) animals#show
PUT /aquariums/:aquarium_id/animals/:id(.:format) animals#update
DELETE /aquariums/:aquarium_id/animals/:id(.:format) animals#destroy
aquariums GET /aquariums(.:format) aquariums#index
POST /aquariums(.:format) aquariums#create
new_aquarium GET /aquariums/new(.:format) aquariums#new
edit_aquarium GET /aquariums/:id/edit(.:format) aquariums#edit
aquarium GET /aquariums/:id(.:format) aquariums#show
PUT /aquariums/:id(.:format) aquariums#update
DELETE /aquariums/:id(.:format) aquariums#destroy
aquarium GET /aquarium(.:format) aquariums#show
Thanks in advance!
Greg
Change that last route from this:
get 'aquarium', to: 'aquariums#show', :as => :aquarium
to this:
get 'aquarium', to: 'aquariums#show', :as => :current_aquarium
The problem is that you have to routes named the same thing:
aquarium GET /aquariums/:id(.:format) aquariums#show
aquarium GET /aquarium(.:format) aquariums#show
If you make the change above then that second route will not match when you make those link to calls... as it stands now, the second one is matching and like the route says is using your argument as the :format.
If you do make this change, you may need to tweak some things if you are intentionally linking to 'current_aquarium'.

Creating Custom Post Method

I'm trying to create a a custom post method but i'm having trouble finding how to start.
What I want to do is to be able to read a CSV file, foreach entry, insert a new row into a database.
In the index file, I want to be able to hit 1 link or button to start a custom method.
This method would open my csv file (traverse each row and insert into database)
So essentially on my index.html.erb I would like to see something like:
<%= link_to "Load CSV to Database", :controller => MyController, :action => MyCustomAction %>
I believe I need to edit my routes.rb and this is where I'm stuck. How do I make it so that my routes know that MyCustomAction is a post.
My Rake Route:
use_database_csv_files POST /csv_files/use_database(.:format) csv_files#use_database
csv_files GET /csv_files(.:format) csv_files#index
POST /csv_files(.:format) csv_files#create
new_csv_file GET /csv_files/new(.:format) csv_files#new
edit_csv_file GET /csv_files/:id/edit(.:format) csv_files#edit
csv_file GET /csv_files/:id(.:format) csv_files#show
PUT /csv_files/:id(.:format) csv_files#update
DELETE /csv_files/:id(.:format) csv_files#destroy
Thanks
You can try:
resources :MyController do
collection do
post 'MyCustomAction'
end
end
This blog post may also help you if you want to do member instead of collection

Rails Routing Error for nested form_for

I now this has been asked a thousand times but that doesn't help me heh :) I've been at this an hour. My form:
= form_for #comment, :url_for => { :action => "create", :controller => "comments"}, :method => :post
my rake routes:
POST /t/:trunk_id/r/:root_id/comments(.:format) {:action=>"create", :controller=>"comments"}
trunk_root_comment GET /t/:trunk_id/r/:root_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
The error:
undefined method `comments_path' for #<#<Class:0x007fed2c713128>:0x007fed2c71cc78>
If I name space the form to:
= form_for [:trunk_root, #comment], :url_for => { :action => "create", :controller => "comments"}, :method => :post do |f|
which should make the route trunk_root_comments_path.. which is correct according to the rake routes.. I get:
No route matches {:controller=>"comments", :format=>nil}
Help is very much appreciated.. been looking at this for hours..
UPDATE:
Thank you Ryan for such a great answer! A very clear explanation of something I was just sort of 'throwing things' at, now at least I understand better. I actually already had 'trunk_root_comments_path' available in my rake routes, and I had tried a couple of the combinations you mentioned, but I wasn't really grocking what I was missing, so you helped. I'm using Mongo and I don't actually have a Trunk model, I just have an attribute on roots called #root.trunk, though I have a trunk controller and therefore its a part of my routes(maybe a bad idea idk).
So I tried your TLDR and it said error:
Undefined method 'root_comments_path'
.. cause no Trunk model exists, I assume?.. so I made #trunk just equal the correct id with
= form_for [#trunk, #root, #comment] do |f|
<- and I got 'undefined method `politics_root_comments_path''.. I figured well.. that probably makes sense.. since I'm failing I must as well try your most explicit version:
= form_for #comment, :url => (trunk_root_comments_path(:trunk_id => #root.trunk, :root_id => #root.id)) do |f|
and sure enough that worked... so I'm not quite sure how to do it shorter then this.. the odd thing for me is I have another nested resource "photos" at the same level of depth in the routes and I was able to get that to work with = form_for [:trunk_root, #photo], :html => { :class => 'root_form' } do |f|.. but here for some reason I couldn't.. anyways I'd say you gave me enough to understand 100% but I think I went from 20% understanding to 50% understanding.. I know now that id's ARE important to routes, and the named helpers need access to them. I got an introduction to how the url_helper works, but would need to read more on it to really grock it fully I think. I'm also now able to construct proper routes in their longer form at least to get through tricky situations like this. So thank you :)
TL;DR You need to specify both a :trunk_id and a root_id in your URL or use form_for like this:
<%= form_for [#trunk, #root, #comment] do |f| %>
Rails is attempting to build a URL from the hash you're giving it, but that hash doesn't match anything in its routing table. You could do this:
{ :controller => "comments", :action => "create", :trunk_id => trunk.id, :root_id => root.id }
But that's really a bit tl;dr.
The cooler way to do it is this:
trunk_root_comments_path(trunk, root)
Where trunk and root are Trunk and Root instances respectively.
Now, if you want to be super-wicked-cool, do it like this:
<%= form_for [trunk, root, comment] do |f| %>
Science!
So how does this work? Elementary, my dear:
Rails first recognises that we're using form_for using an Array and that we mean business. Rails uses this array passed in and builds a URL out of it. It does this by using the routing helpers that are defined by the routes. Unfortunately, you've defined your routes in a funny way that don't play nice with this, but don't fear! We can fix this.
The way you can do it is this where you have this in config/routes.rb:
post '/t/:trunk_id/r/:root_id/comments'
Instead put this:
post '/t/:trunk_id/r/:root_id/comments', :as => "trunk_root_comments"
You may alternatively already have this:
match '/t/:trunk_id/r/:root_id/comments', :via => :post
Which should become this:
match '/t/:trunk_id/r/:root_id/comments', :via => :post, :as => "trunk_root_comments"
Either way, you've now got not one, but two(!!) path helpers defined by the routes. These aretrunk_root_comments_path and trunk_root_comments_url respectively. The names of these methods are super important for what I am about to explain to you. Pay attention!
So, back to our little form_for call:
<%= form_for [trunk, root, comment] do |f| %>
Rails knows that we're using an Array because it can see it. What it does with this Array may seem like magic, but isn't really.
Rails will take each element of this array and build a routing helper method name up from the different parts. This isn't actually part of form_for, but another method called url_for that you can use by itself:
url_for([trunk, root, comment])
In the beginning, this routing helper method name generated by url_for is simply an empty array ([]). Nothing special at all.
But then what happens is special!
The first element is going to be a persisted instance of the Trunk class. By "persisted" I mean that it's an object that maps directly to a record in the database. Yay ORMs!
Rails will know this, and so will turn the routing helper into this: [:trunk].
The second element is going to be a persisted instance of the Root class. Rails also knows this (damn, Rails is smart!) and will then append this to the array, turning it into [:trunk, :root]. Awesome.
The third (and final) element is then checked by Rails. It sees that (in this case) it's a non-persisted element, i.e. it's not been saved to the database.. yet. Rails treats this differently and will instead append [:comments] to the array, turning it into this:
[:trunk, :root, :comments]
See where I'm going with this now?
Now that Rails has done it's thing (or thang, if you like) it will join these three parts together like this: trunk_root_comments, and just for good measure it'll put _path on the end of it, turning it into the final trunk_root_comments_path helper.
And then! Man, and then... Rails calls this method and passes it arguments! Just like this:
trunk_root_comments_path(:trunk_id => trunk.id, :root_id => root_id)
This generates a full path to the resource like this:
/t/:trunk_id/r/:root_id/comments
And bam! Full circle! That's how Rails will know to generate the URL and you don't have to use ugly hashes anymore.
Success!
Not sure if you have this route set up but try:
= form_for #comment, :url => trunk_root_comments_path

Beginner struggling with update_attribute command

I am in the process of trying to use the update_attribute command, but struggling to get it working (at all) and hoped someone could point me in the right direction?
I have previously posted a question about this issue, it was very useful in terms of giving a feel for the mechanics of what is going on, but unfortunately it didn't actually get it working.
I have a database of items (Items), which among other things contains ':item_name', ':click_count' and ':external_url'.
Currently I have a view (Showselecteditems) in which there is a list of all the items, when a user clicks on an item name, they are directed to the appropriate external url. This works using the code:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url %>
I would now like to add the ability to count the number of times a particular item name has been clicked on (i.e. in total for all users, not individual users) and therefore the number of times each external url has been visited in order to work out which is most popular.
Reading around, I believe i need to modify the code above to something of the form:
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And need to define this function somewhere - it seems to only be found if located in 'application_helper'?
def clickcountplusone
clickeditem = Items.find(params[:identifier])
clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end
Needless to say, I cannot get this to work... My question is therefore, how can I set things up correctly so that when the link is clicked on the count is incremented? The other common problem people seem to report is that the number will be incremented each time the page is refreshed, which I would like to avod if possible.
Previously people have suggested adding to the 'show' section of the 'Items' controller, however, i don't know how this would work as the links are being clicked on the Showselecteditems view page, not the database itself where you get the show, edit, destroy commands. Any advice greatly appreciated.
This
<%= link_to selecteditem.item_name.to_s, selecteditem.external_url, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
will not point user to the some_controller#clickcountplusone, because you already specified an external link.
The easiest way to do this job is to modify your link_to like:
<%= link_to selecteditem.item_name.to_s, {:controller => params[:controller], :action => clickcountplusone, :identifier => selecteditem.item_name} %>
And then to modify your actions source:
def clickcountplusone
clickeditem = Items.find(params[:identifier])
redirect_to clickeditem.external_url if clickeditem.update_attribute(:click_count, clickeditem.click_count + 1)
rescue ActiveRecord::RecordNotFound # to avoid error if no identifier value
end