Link to remote and the equivalent in Rails 3

Hi

WOW! Is Rails 3 another interesting learning curve. Just when I think
I have a general working knowledge of Rails 2 … things change.

I’m trying to replicate the following in Rails 3:

<%= link_to_remote image_tag(“creditcard.png”, :border => 0), :url =>
{:action => :make_payment, :type => “Credit”, :id =>
@appt.id }, :with=>"‘amount=’+$F(‘amount’)", :complete =>
remote_function( :url => { :action => :load_appt, :id => @appt.id })
%>

Obviously, when I click a link, I want some action and controller to
trigger and something to happen to the page that the link is part of.

I’ve watched what feels like EVERY railscast on the internet and read
every Rails 3 tutorial out there. I’ve got a basic understand of UJS
and see the benefits etc … but all I want to do is click a link,
some controller action trigger, a render :update do |page| call to
trigger and then I’ll be happy!!

But no luck so far …

So … with the following line:

<%= link_to “Add Photos”, entries_path, {:controller
=> :entries, :action => :showphotos, :remote => true} %>

I get the following error:

Started GET “/entries” for 127.0.0.1 at Fri Aug 13 23:13:18 +0100 2010
Processing by EntriesController#index as JS
Rendered entries/index.js.erb (0.2ms)
Completed 200 OK in 15ms (Views: 15.1ms | ActiveRecord: 0.0ms)
Started GET “/images/test.png” for 127.0.0.1 at Fri Aug 13 23:13:18
+0100 2010
ActionController::RoutingError (No route matches “/images/test.png”):
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0.rc/lib/
action_dispatch/middleware/templates/rescues/routing_error.erb within
rescues/layout (0.6ms)

It’s calling a GET to /entries and only calling some JS I have in
index.js …

Not calling the showphotos action.

This is really holding me back now as I can’t for the life of me find
a solution or something which makes sense.

Any ideas please from knowledgeable Rails 3 gurus?

Cheers

Darrne

On Fri, Aug 13, 2010 at 5:17 PM, Ruby on Rails: Talk
[email protected] wrote:

I’m trying to replicate the following in Rails 3:

<%= link_to_remote image_tag(“creditcard.png”, :border => 0), :url =>
{:action => :make_payment, :type => “Credit”, :id =>
@appt.id }, :with=>“‘amount=’+$F(‘amount’)”, :complete =>
remote_function( :url => { :action => :load_appt, :id => @appt.id })
%>

link_to ‘foo’, some_path, :remote => true


Greg D.
destiney.com | gregdonald.com

You should probably add more information. like the routes for entries
and what exactly is calling /images/test.png.

On Aug 13, 6:17 pm, “Ruby on Rails: Talk” [email protected]

Thanks for the speedy reply.

I’ve tried … <%= link_to “Add Photos”, :controller
=> :entries, :action => :showphotos, :remote => true %>

Which gives … the following error:

ActionView::Template::Error (No route matches
{:action=>“showphotos”, :remote=>true, :controller=>“entries”}):
6:

Date: <%= @entry.entrydate %>

7: Title: <%= @entry.title %>

8: Country: <%= @entry.country.name %>


9: <%= link_to “Add Photos”, :controller => :entries, :action
=> :showphotos, :remote => true %>
10:

ADD OPTIONS TO DELETE AND EDIT AND OTHER SUCH EXCITING
THINGS HERE


11:

12:

app/views/entries/show.rhtml:9:in
`_app_views_entries_show_rhtml___424117329_2183052120_0’

Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0.rc/lib/
action_dispatch/middleware/templates/rescues/_trace.erb (0.8ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0.rc/lib/
action_dispatch/middleware/templates/rescues/_request_and_response.erb
(168.2ms)
Rendered /Library/Ruby/Gems/1.8/gems/actionpack-3.0.0.rc/lib/
action_dispatch/middleware/templates/rescues/template_error.erb within
rescues/layout (173.2ms)

And if I change it to … <%= link_to “Add Photos”, :url =>
{:controller => :entries, :action => :showphotos}, :remote => true %>

I get the following …

Started GET “/entries/1?
remote=true&url[action]=showphotos&url[controller]=entries” for
127.0.0.1 at Fri Aug 13 23:36:30 +0100 2010
Processing by EntriesController#show as HTML
Parameters: {“url”=>{“action”=>“showphotos”,
“controller”=>“entries”}, “id”=>“1”, “remote”=>“true”}
Entry Load (0.2ms) SELECT entries.* FROM entries WHERE
(entries.id = 1) LIMIT 1
Country Load (0.9ms) SELECT countries.id, countries.name,
countries.created_at, countries.updated_at FROM countries
WHERE (countries.id = 10) LIMIT 1
Photo Load (0.3ms) SELECT photos.id, photos.entry_id,
photos.created_at, photos.updated_at,
photos.data_file_name, photos.data_content_type,
photos.data_file_size, photos.data_updated_at,
photos.data_comment FROM photos WHERE (photos.entry_id = 1)
Rendered entries/show.rhtml within layouts/application (103.0ms)
Completed 200 OK in 116ms (Views: 106.0ms | ActiveRecord: 1.4ms)

Which still suggest the controller/action is not being run …

I’m still none the wiser!

Any clarification?

Thanks

Darren

Hi

The /images/test.png is nothing … I just need to create a file (paper
for swfupload and paperclip).

I have the following in my routes file:

resources :admin
resources :countries
resources :entries
resources :photos

Is this not enough?

Oh … And if I change it to … <%= link_to “Add Photos”, ‘/entries/
showphotos’, :remote => true %>

I get …

Started GET “/entries/showphotos” for 127.0.0.1 at Fri Aug 13 23:37:45
+0100 2010
Processing by EntriesController#show as JS
Parameters: {“id”=>“showphotos”}
Entry Load (0.2ms) SELECT entries.* FROM entries WHERE
(entries.id = 0) LIMIT 1
Completed in 8ms

ActiveRecord::RecordNotFound (Couldn’t find Entry with ID=showphotos):
app/controllers/entries_controller.rb:4:in `show’

Hence why I’m slowly going insane …

Maddeningly Darren

Try watching this Railscast…

I’m not sure what your entry controller is doing. If you’re trying to
add a photo wouldn’t you be posting to the photos controller instead
of doing a get on the entries controller?

<%= link_to “Add Photos”, photos_path, :method => :post, :remote =>
true %>

anyway… I too many questions but that railscast is pretty useful

Thanks for the advice.

But I’ve watched it probably 5 times … even in slow motion! And it
doesn’t answer the problem.

The Entries Controller just shows Blog Entries. I click on a link
which says ‘Add Photos’ and all I want it to do is execute some
controller code and then update a div.

The Photos thing happens elsewhere and works perfectly (well uploading
and saving the photos).

But my original problem still stands. I cant get the link_to to
execute some code in an controller/action.

So any more ideas anyone?

On Aug 13, 2010, at 6:37 PM, Ruby on Rails: Talk wrote:

Thanks for the speedy reply.

I’ve tried … <%= link_to “Add Photos”, :controller
=> :entries, :action => :showphotos, :remote => true %>

I think you might be missing that there are two (2) hashes:

<%= link_to “Add Photos”,
{:controller=>:entries, :action=>:showphotos}, {:remote=>true} %>

The other answers that you’re seeing use a named route for the second
argument (which I’d recommend, too, but this might help you get going
with a smaller step).

match ‘/entries/showphotos’, :to => ‘entries#showphotos’, :as =>
‘showphotos’

<%= link_to “Add Photos”, showphotos_path, :remote => true %>

-Rob

10:

ADD OPTIONS TO DELETE AND EDIT AND OTHER SUCH EXCITING
(168.2ms)
remote=true&url[action]=showphotos&url[controller]=entries" for
photos.created_at, photos.updated_at,
Any clarification?

Greg D.
.

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

Thanks for the advice Rob and SOOOO sorry to keep harping on about
this … but I’ve tried:

<%= link_to “Add Photos”, {:controller => ‘entries’, :action =>
‘showphotos’}, {:remote => true} %>

and

<%= link_to “Add Photos”, {:controller => :entries, :action
=> :showphotos}, {:remote => true} %>

and both times I’m getting the following error:

Showing /Users/dazzaroonie/Sites/newwt/app/views/entries/show.rhtml
where line #9 raised:

No route matches {:controller=>“entries”, :action=>“showphotos”}
Extracted source (around line #9):

6:

Date: <%= @entry.entrydate %>

7: Title: <%= @entry.title %>

8: Country: <%= @entry.country.name %>


9: <%= link_to “Add Photos”, {:controller => ‘entries’, :action =>
‘showphotos’}, {:remote => true} %>
10:

ADD OPTIONS TO DELETE AND EDIT AND OTHER SUCH EXCITING THINGS
HERE


11:

12:

Which is rubbish as the following the entries controller:

class EntriesController < ApplicationController

def show
@entry = Entry.find(params[:id])
end

def new
@countries = Country.find(:all, :order => :name)
@entry = Entry.new
end

def create
@entry = Entry.new(params[:entry])
if @entry.save
flash[:notice] = “Successfully saved the Entry”
redirect_to entry_path(@entry)
else
redirect_to :new
end
end

def showphotos
logger.warn(“I am here”)
end

end

I’m going to end up in the nut house soon lol.

Any further ideas?

On Aug 14, 8:14 pm, Rob B. [email protected]

Darren,

If you look at the log here, you’ll see the problem.

Started GET “/entries/showphotos” for 127.0.0.1 at Fri Aug 13 23:37:45

+0100 2010
Processing by EntriesController#show as JS
Parameters: {“id”=>“showphotos”}
Entry Load (0.2ms) SELECT entries.* FROM entries WHERE
(entries.id = 0) LIMIT 1
Completed in 8ms

As you’re using restful routes, you get a number of routes for free by
putting in resources :entries. When you supply your url by putting in
the action and controller, it comes across and rails things you’re
trying to hit the show action because you’re supplying the first part
“/entries” followed by a second item that it doesn’t know, so it hits
the “show” action. That’s why you see the line where it tries to pull
up an entry with id of 0 (“showphotos.to_i” == 0).

To solve your problem, put a named route ABOVE the resource :entries
line. As previously suggested, you could add this line.

match ‘/entries/showphotos’, :to => ‘entries#showphotos’, :as =>
‘showphotos’

However, you won’t know what entry you’ll be adding photos to so be
sure to add an ID as well.

match ‘/entries/showphotos/:id’, :to => ‘entries#showphotos’, :as =>
‘showphotos’

Just remember, you’ll need to put the more specific routes higher as
they get higher priority.

  • Robert

On Aug 13, 6:39 pm, “Ruby on Rails: Talk” [email protected]

I think it might be “solved” by including :method => :post as below …

<%= link_to “Add Photos”, {:controller => ‘entries’, :action =>
‘showphotos’}, {:method => :post, :remote => true} %>

Then it seems to work …

It also works with:

<%= link_to “Add Photos”, showphotos_path, :method => :post, :remote
=> true %>

Thanks for all the help.

I’m not really sure why I need to include the :method => :post
thing … but I’m sure it’ll all become clear …