I'm having trouble with a route that seems to be right in the console but gives me a routing error when I use it in the server. The case is similar to the "edit" and "update" pair. Calling GET 'messages/25/followup' should route to messages#followup, while the same URL with POST should route to messages#followup_send. In my routes.rb file I have
get "messages/:id/followup", :to => "messages#followup"
match "messages/:id/followup", :to => "messages#followup_send", :via => :post
Displaying the routes gives
ruby-1.9.2-p0 :092 > puts fu
GET /messages/:id/followup(.:format) {:controller=>"messages", :action=>"followup"}
POST /messages/:id/followup(.:format) {:controller=>"messages", :action=>"followup_send"}
Testing in the console gives
ruby-1.9.2-p0 :088 > r.recognize_path "/messages/54/followup", :method=>'POST'
=> {:controller=>"messages", :action=>"followup_send", :id=>"54"}
The code in the form is
<form id="edit_message_42" class="edit_message" method="post" action="/messages/42/followup?method=post" accept-charset="UTF-8">
...
<input type="submit" value="Send" name="commit">
However, if I click on the button I get in the log
Started POST "/messages/42/followup?method=post" for 127.0.0.1 at 2012-06-27 13:54:48 +0100
ActionController::RoutingError (No route matches "/messages/42/followup")
The same thing happens if I enter the URL manually (including the "method=post'). I will just get around this for now by using a separate name (e.g. /messages/42/send_followup) rather than relying on the GET-POST distinction, but I would like to understand what is going on here.
Thanks for any ideas.
Related
I have this in my Rails controller:
def download_clip
send_file "public/output.mp4", :type=>"video/mp4", :filename => "output.mp4", :disposition => 'attachment'
end
and in my HTML code I have this:
Now could somebody tell me why Firefox's download window will NOT pou up, but chrome downloads the file fine? Instead firefox opens a new window and starts playing the file. I WANT THE DOWNLOAD BOX to POPUP. I have spend too much time on it
You are using a relative url, which may not map correctly depending on the page it is used.
Try changing your link to:
<%= link_to "some text", :controller => :your_controller_name, :action => :download_clip %>
If this doesn't help, check if the Content-Diposition response header is being set as 'attachment'. If it is, then the problem is likely with your own Firefox environment and not with the server. Resetting Firefox to defaults should fix that...
Add
headers['Content-Disposition'] = "attachment;"
in your download_clip action..
I've got a set of routes that looks like this:
resources :placements do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
This produces routes that behave like you would expect:
/placements/1234/foo
/placements/1234/bar
However, I also need "generic" routes for a few methods that do not need an individual placement. So, I build a routes block that looks like this:
resources :placements do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
match '/placements/baz' => "placements#baz"
If I rake routes, I get a route that looks good:
/placements/baz
Note the lack of an id. However, if I try to visit that route, Rails tries to call the show method on the controller instead, as if "baz" was an ID, instead of a method name. How can I build a routing structure that gives me what I am after, without having to change the first segment of my route (placements), to something else?
Move the second route over the resources block ie,
match '/placements/baz' => "placements#baz"
resources :placements, :id => /\d+/ do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
or add a regex for the id in resources, ie something like:
resources :placements, :id => /\d+/ do
match '/foo' => "placements#foo"
match '/bar' => "placements#bar"
end
match '/placements/baz' => "placements#baz"
So I realize that Rails mailers need help in generating urls. I want HTML email with images served externally (no attachments) so I've set both:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_mailer.asset_host = "http://localhost:3000
and two things happen in my mailer template:
It hates the use of image_tag. Just barfs with an action view template error
url_for does nothing for unnamed routes. shouldn't it prepend the asset_host? for assets in public?
So I have a simple ajax call to a page:
= link_to 'click me', my_path, :onclick => "if ($('#go').val() == "ok") { alert('going'); } else { return false; }", :remote => true do
This works just fine, I see the alert only when my field with id "go" has ok in there... but the issue is that the remote action triggers every time no matter what.
If this was not a remote link it would work just fine, not going through with the link, but it does not seem to behave the same way with a :remote => true ?
How can I achieve the expected result ?
Thanks,
ALex
The issue here is that the Rails UJS driver will see the data-remote and then perform the action because of a function like this in rails.js so perhaps try setting the property om your link inside the JS and remove the :remote => true. That might work however I dont know if rails.js would bind to that correctly or not.
Also, consider placing this JS in the application.js once you're done debugging just so you dont have inline JS all over your controllers.
All.
A Rails n00b here...
I'm writing an application that reports the status of a transaction.
Some of the content in the rendered HTML comes from instance variables
initialized in the controller, while other content comes from text files
(e.g., log files) that I want to render in the HTML using <pre> tags.
What is the "Rails Way" to do this?
Thank you for your time...
<pre>
<%= render :file => '/tmp/test.log' %>
</pre>
Here you go: http://edgeguides.rubyonrails.org/layouts_and_rendering.html
In some cases (when the file is not small and loading it is connected with a delay), I prefer to load the page content and then to use jQuery ajax request to load the file content.
For example, let's say I have a model with file path attribute. In the view layout I am doing something like this:
<pre data-source=" <%= (#file.path) %>"></pre>
Then in the corresponding js file I am loading the context like this:
$(document).ready ->
$.ajax(
url: $("pre").data("source")
context: document.body
).done (response) ->
$("pre").html response
return
return
Of course you can check the jQuery ajax documentation for more options. For example, you can render the pre tag with loading like this:
<pre data-source=" <%= (#file.path) %>"><div class="loading"></pre>
or use other jQuery animations as well.