Where default protocol is configured for rails `link_to`? - ruby-on-rails

Rails link_to generates a link with http:// prfeix. But site is https.
So forms must not be used like so. And actually it is not working for most browsers.
The problem appears in spree shop. Internal templates of spree generates wrong urls. So modify spree internal templates to add protocol to each link_to is not a solution.
Where default protocol is configured for rails link_to?

You need to pass :protocol => 'https' inside the url creation.
<%= link_to "text", your_url(:protocol => "https"), :method => :post %>

Related

Rails: configuring a form action's host using custom URL from settings

I have a Rails app that I am feeding cross domain in production. It needs absolute references. Because of this, I have enabled the following in my config/environments/production.rb:
config.action_controller.asset_host = "http://myapp.herokuapp.com"
That works fine for images and resources but my input form that looks like this:
<%= form_tag('/plans/collapse_plans', :method => 'post', :remote => true ) do %>
is still getting this in the console:
Failed to load resource file://localhost/plans/collapse_plan
How can I change it so that form action will automatically include the specified host, instead of defaulting to localhost? Can I set this anywhere in config?
This seems like it will work:
https://github.com/binarylogic/settingslogic
Then I can just do:
<%= form_tag mysettings.myspecifiedhost + plans_collapse_plans_path, :method => 'post', :remote => true do %>
I may be on the wrong track here, but:
Asset host is not your application's host, asset host is a host that serves you /app/assets folder and this is configurable so you can set up a CDN for example, it's not intended for hosting action points.
If you want to target the full url of your own host use rake routes to get the route name corresponding to /plans/collapse_plans which probably looks something in the lines of plans_collapse_plans and then you can use plans_collapse_plans_url and rails will render the correct full URL for you.
If you're using the default host name rails provides automagically this will "just work", i.e.
[2] pry(#<#<Class:0x000000048fd780>>)> account_edit_url
=> "http://dev:3000/account/edit"
If this doesn't "just work", you can override all url helpers in the app by overriding default_url_options in your ApplicationController:
def default_url_options
{:host => HOST}
end
and be sure to set the HOST constant in your application's environment, for example:
[1] pry(#<#<Class:0x00000005047d10>>)> account_edit_url
=> "http://o7ms:3000/account/edit"
If you need to override this just in certain situations you can leave the ApplicationController alone and do:
[3] pry(#<#<Class:0x000000048fd780>>)> account_edit_url(host: MY_HOST_FOR_THE_OTHER_THINGY)
=> "http://foo:3000/account/edit"
In all cases you'll set up a config option in one place and all endpoints in the app will adjust.
EDIT
If you want to go fancy,
see default_url_options and rails 3,
by overriding url_options you may be able to implement pretty calls like account_edit_url(ajax_host: true), the url_options method would look something like this if this works:
def url_options
options = super
if super.delete(:ajax_host)
{host: AJAX_HOST}.merge(options)
else
options
end
end
what you are trying cannot be done normally for ajax calls.
see http://en.wikipedia.org/wiki/Same-origin_policy
Two approaches:--
1.) <%= form_tag root_url + plans_collapse_plans_path, :method => 'post', :remote => true do %>
concatenation:-- root_url + plans_collapse_plans_path
2.) in config/environments/production.rb
MyApp::Application.configure do
# general configurations
config.after_initialize do
Rails.application.routes.default_url_options[:host] = 'root_url' #'localhost:3000'
end
end

How can I create link to the json-view of an object in Rails?

What do I need to change in the code below to make it link to a view rendered as json?
<%= link_to 'JSON Link', #mymodel %>
So I'd like to generate the following url
http://localhost:3000/mymodels/1.json
instead of
http://localhost:3000/mymodels/1
(These urls both work as expected.)
To do this, you must specify the format:
<%= link_to 'JSON Link', your_model_path(#mymodel, :format => 'json') %>
The URL helper methods can be retrieved by running:
rake routes
The first column is the name of the helper method, on which you should append either _path or _url, the latter will generate an absolute URL.
More information is in the Guide To Rails Routing

Ruby on Rails: link-to using post method but parameters are in the URL

I'm using
link_to 'My link', path(:arg1 => session[:arg1], :arg2 => session[:arg2],:arg3 => anyobject.id), :method => :post
but the HTML link being generated includes (arg1,arg2,arg3) as URL query parameters.
How can remove them? Did I miss something in the documentation?
A link_to will always put the arguments into the query string as it is creating a get-style HTML link - even if you put :method => :post that just appends an extra ("special") argument _method.
What I think you really want is a button_to link - which will make it into a sort of form-post. It works the same, but it says button_to instead (for example, button_to 'My link', path(:params => :go_here). The downside is that it will look like a button. But you can give it a CSS class (eg "unbutton") and then change the styling on that CSS class to make it not look like a button.
Alternatively, if what you really want is actually to have no params passed to the controller at all... then just don't include them when making your link (for example, link_to "My link" path - there's no need for :post if you don't want to post any params).
Finally, if what you want is for the params to become a part of the URL (for example, stuff/[param_1]/more_stuff/[param_2], etc.) then you need to update your routes to include these parameters as options. Have a look at the routing section of the rdoc for how to do that.
You can use below code, which rails.js need data-method to switch to post mode in Ajax.
<%= link_to '<button id="print" type="submit" class="btn">Print</button>'.html_safe, { controller: :lots, id: #lot.containername, action: "print_#{print_template}", remote: true }, {'data-method' => :post} %>

Where did link_to_function disappear to in Rails 3?

I was just playing around with Rails 3 beta and noticed that link_to_function is now gone. I presume there's an alternate method of achieving the same result (onclick event?) but I was wondering if there's a more Rails-3'y way of doing it. TIA.
Rails 3 seems to have done away with Prototype Helper in favour of a less obtrusive/JS library agnostic approach. The goal is to eliminate all inline javascript generated by Rails. Prototype Helper generated pretty much all of the javascript.
Now any of the non remote variants of helpers will generate the proper javascript for a remote call in your JS library of choice just by supplying the :remote => true option.
Unfortunately this doesn't hold true for the x to function methods. For the time being there are the prototype legacy helpers which are no longer a core part of Rails.
You could also use call as defined in ActionView::Helpers::PrototypeHelper::JavascriptGenerator::GeneratorMethods to supply javascript code to :onclick as an html_option for link_to, but that's not exactly pretty.
Examples:
Rails < 3 | Rails 3
link_to_remote "target", url | link_to "target", url, :remote => true
form_remote_for #post | form_for #post, :remote => true
etc....
Or something to that effect. I'm having trouble finding official documentation to back up my claims. So the release notes will have to do for now.
Before you can use it, you'll need to include the proper js source files. Ensure that you are loading prototype.js and rails.js, or the library and driver for JS framework of choice in their place.
Remember, Rails 3 is in beta now. But that doesn't mean it's done. I honestly have no idea how link_to_function will be replaced. It goes against the ideal of unobtrusive javascript.
To answer my own question, seems this works and is sufficient for what I need:
link_to "name", nil, :onlick => "alert('Hello, world!')"
link_to_remote can be done like this
link_to "target",:remote => true
and to do a ajax post/get you have
link_to "target", {:controller =>
controller, :action => method, }, :remote => true
In rails 2 it was like this
link_to_remote "target", :url =>
{:controller => controller, :action =>
method, }, :remote
=> true

Modifying rails route helper

I'd like to modify the behavior of the rails route helper *_url for a single route/page.
Here's what I'm try to do:
User visits:
http://test1.myapp.com/account
All the *_url routing helpers resolve to http://test1.myapp.com/ as normal.
But, then if the user goes to https://myapp.heroku.com/account/billing?id=test1
I'd like all the *_url routing helpers on that page to resolve to: http://test1.myapp.com/
instead of http://myapp.heroku.com/
So, is it possible to change the domain bit for all the *_url helper calls for a specific page?
For those interested, I'm trying to use heroku's piggyback ssl method for my app for just a secured billing page.
You can actually just modify the links that point to the billing area:
<%= link_to "Billing", my_helper_url(test1, :host => "myapp.heroku.com", :protocol => "https") %>

Resources