Rails 3.1 中的 link_to

发布于 2024-12-09 18:55:06 字数 2606 浏览 1 评论 0原文

我正在将应用程序从 Rails 2 转换为 Rails 3,有人可以帮助我处理这一小段代码吗? link_to 不起作用,有人可以告诉我如何在 Rails 3.1 中正确使用 link_to 而不是 link_to_remote 吗? Rails 2 代码

<%= 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 代码

<%= 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
                    }
%>

我用 .js.erb 替换所有 .rjs 文件。这是我在 Rails 3 中得到的 URL:

<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>

这是在 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>

我的控制器

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

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&url%5Bcontroller%5D=cmn%2Fpopup&url%5Bframe_height%5D=355px&url%5Bframe_url%5D=%2Fcommon%2Flogin%2Fen%2Fsentry%3Fapp_action%3Dpackage.item.edit%26id%3D3%26remote%3Dtrue&url%5Bframe_width%5D=570px&url%5Bremote%5D=true">3</a>

This is in Rails 2:

<a href="#" onclick="new Ajax.Request('/cmn/popup/show_popup?frame_height=355px&frame_url=%2Fcmn%2Fcmn%2Findex%2F2%3Fapp_action%3Dpackage.item.edit%26amp%3Bbrand%3Dsentry%26amp%3Blanguage%3Den&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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的笑 2024-12-16 18:55:06

请看一下 Rails 3 中 link_to 的语法:
http://api.rubyonrails.org/classes/ActionView /Helpers/UrlHelper.html#method-i-link_to

您将所有参数放在 :url 哈希中,但不需要命名它是 :url,只需在哈希中传入选项即可,如下所示:

<%= 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
%>

记住从 url 哈希中获取 :remote

让我知道这是否有效。

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.

悍妇囚夫 2024-12-16 18:55:06

这是我第一次看到将路径定义为 :controller 参数的字符串。

我的意思是 de :controller => 'cmn/弹出窗口'。这对我来说是新的,感觉很奇怪。

您确定这有效并且正确的控制器和操作正在接收请求吗?

我认为这可能很棘手的另一件事是控制器上的渲染调用。只需打电话

render :layout => false

,或者根本不打电话。

如果模板与操作同名,并且放置在与控制器类似的目录中,则 Rails 会根据请求类型知道需要渲染什么模板以及扩展名 (js/html/xml)。也许渲染:模板=> .... 强制渲染 html 模板。

我认为 :nolayout 选项无效。无论如何,如果请求是针对 javascript 文件,它永远不会呈现布局。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文