link_to in Rails 3.1

1.3k Views Asked by At

I'm converting an application from rails 2 to rails 3 and could somebody please help me regarding this small bit of code. The link_to is not working , could some one point me how to use link_to instead of the link_to_remote in rails 3.1 properly? Rails 2 code

<%= link_to_remote  package_item.getId(),
                    :url => { :controller => 'cmn/popup',
                              :action     => "show_popup",
                              :frame_url  =>  admin_url( 
                                                :ctrl   => controller,
                                                :app_action => 'package.item.edit',
                                                :id     => package_item.getId().to_s,
                                                :remote => true
                                              ),
                              :frame_width  => '570px',
                              :frame_height => '355px'
                            }
%>

Rails 3.1 code

<%= link_to package_item.getId(),
            :url => { :controller => 'cmn/popup',
                      :action     => "show_popup",
                      :frame_url  =>  admin_url( 
                                        :ctrl => controller,
                                        :app_action => 'package.item.edit',
                                        :id   => package_item.getId().to_s
                                      ),
                      :frame_width  => '570px',
                      :frame_height => '355px',
                      :remote       => true
                    }
%>

I replace all .rjs file with .js.erb. This is the URL I'm getting in Rails 3:

<a href="/common/login/en/sentry?url%5Baction%5D=show_popup&amp;url%5Bcontroller%5D=cmn%2Fpopup&amp;url%5Bframe_height%5D=355px&amp;url%5Bframe_url%5D=%2Fcommon%2Flogin%2Fen%2Fsentry%3Fapp_action%3Dpackage.item.edit%26id%3D3%26remote%3Dtrue&amp;url%5Bframe_width%5D=570px&amp;url%5Bremote%5D=true">3</a>

This is in Rails 2:

<a href="#" onclick="new Ajax.Request('/cmn/popup/show_popup?frame_height=355px&amp;frame_url=%2Fcmn%2Fcmn%2Findex%2F2%3Fapp_action%3Dpackage.item.edit%26amp%3Bbrand%3Dsentry%26amp%3Blanguage%3Den&amp;frame_width=570px', {asynchronous:true, evalScripts:true}); return false;">2</a>

my controller

    def show_popup
      @content_data = {}
      @content_data.merge!(params)
      render(:template => 'cmn/popup/show_popup', :nolayout => 1)
  end
2

There are 2 best solutions below

1
On

Please look at the syntax of link_to in Rails 3: http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

You have all your parameters in a :url hash, but you don't need to name it :url, just pass in the options in a hash, like this:

<%= link_to package_item.getId(),
        { 
                  :controller => 'cmn/popup',
                  :action     => "show_popup",
                  :frame_url  =>  admin_url( 
                                    :ctrl => controller,
                                    :app_action => 'package.item.edit',
                                    :id   => package_item.getId().to_s
                                  ),
                  :frame_width  => '570px',
                  :frame_height => '355px'
        },
        :remote       => true
%>

Remember to get :remote out of the url hash.

Let me know if this works.

0
On

It is the first time I see a string defining a path as a :controller param.

I mean de :controller => 'cmn/popup'. This is new to me, and it feels strange.

Are you sure this works and the request is being received by the correct controller and action?

Another thing i think it can be tricky is the render call on the controller. Just call

render :layout => false

or maybe dont call anything at all.

If the template has the same name than the action and it is placed in a directory named like the controller, rails knows what template needs to be rendered and the extension (js/html/xml) based on the request type. Maybe render :template => .... forces to render an html template.

The :nolayout option i think it is invalid. Anyway, if the request is for a javascript file, it never renders layout.