标签: link-to

如何将变量link_to定义到外部URL

在我的网站上,用户拥有个人资料,其中包含指向其个人外部网站的链接.我存储在名称网站下的postgresql数据库中的网站的网址.当我测试结果时,我总是得到这样的网址:

http://localhost:3000/www.example.com
Run Code Online (Sandbox Code Playgroud)

代替 http://www.example.com

我的观点index.html.erb如下所示:

<% provide(:title, 'All projects') %>
<h1>All projects</h1>

<%= will_paginate %>

<ul class="microposts">
    <%= render @microposts %>
</ul>

<%= will_paginate %>
Run Code Online (Sandbox Code Playgroud)

和我的_micropost.html.erb这样:

<li>
    <span class="title"><%= micropost.title %></span>
    <span class="website"><%= link_to micropost.website, micropost.website %></span>
    <span class="content"><%= micropost.content %></span>
    <span class="timestamp">
        Posted <%= time_ago_in_words(micropost.created_at) %> ago.
    </span>
</li>
Run Code Online (Sandbox Code Playgroud)

我不知道在这种情况下有什么问题.如果我在micropost.website之前设置一个@,它会给我一个错误的未定义方法`website'为nil:NilClass

有没有人可以帮助我(我是RoR初学者)?

KR,Fabian

ruby-on-rails link-to ruby-on-rails-3

33
推荐指数
2
解决办法
4万
查看次数

向link_to添加一个类正在打破链接

我在RoR 3中使用link_to

当我像这样使用它时,它工作正常:

<%= link_to "Add to your favorites list",:controller => 
            'favourite_companies', :action =>'create', 
            :company_id=>"#{@company.id}",   
            :company_name=>"#{@company.company_name}" %>
Run Code Online (Sandbox Code Playgroud)

但我也希望通过课程

但是,这对我不起作用.该课程有效,但它打破了链接.有任何想法吗?

<%= link_to "Add to your favorites list",{:controller => 
            'favourite_companies', :action =>'create'}, 
            :company_id=>"#{@company.id}",   
            :company_name=>"#{@company.company_name}",
            :class=>"ui-button-text button_text"}  %>
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails link-to

30
推荐指数
3
解决办法
8万
查看次数

Rails link_to具有内联样式

我必须在不使用CSS类的情况下更改link_to标签颜色,我该怎么办?我尝试过像

<%= link_to item.description, {}, {:style=>'color:#FFFFFF;', :class => "css_class"} %>
Run Code Online (Sandbox Code Playgroud)

但它不适用于ruby 1.9.2和rails 3.1

ruby-on-rails link-to

29
推荐指数
4
解决办法
5万
查看次数

Ruby on Rails link_to with put Method

我是Rails的新手,我正在尝试使用link_to帮助程序创建一个发出PUT请求而不是GET请求的链接.具体来说,我正在尝试创建一个链接,从管理员面板激活我的应用程序中的用户帐户.我正在使用Rails 3.0.5.

我的routes.rb文件有:

match '/admin/users/:id/activate' => 'admin#activate_user',
  :action => :activate_user, :via => :put
Run Code Online (Sandbox Code Playgroud)

我的观点是:

link_to 'Activate', :action => :activate_user, :id => user.id, :method => :put
Run Code Online (Sandbox Code Playgroud)

但是,这会生成/admin/users/7/activate?method=put带有源代码的URL(例如)<a href="/admin/users/7/activate?method=put">Activate</a>.

相反,我想生成 <a href = "/admin/users/7/activate" data-method="put">Activate</a>

我意识到我可以使用button_to,但我一直在努力解决这个问题,我很困惑为什么我看到这种行为,当其他教程说我正在做的事情应该是有效的.如何创建一个具有我想要的行为的link_to帮助器?

ruby-on-rails link-to

23
推荐指数
2
解决办法
2万
查看次数

link_to更新(没有表格)

我想要一个更新资源的链接,而不使用HTML表单.

路线:

resources :users do
resource :profile, :controller=>"profiles"
 resources :friends
end    
Run Code Online (Sandbox Code Playgroud)

耙路线:

 user_friend GET /users/:user_id/friends/:id(.:format){:action=>"show", :controller=>"friends"}
             PUT /users/:user_id/friends/:id(.:format){:action=>"update", :controller=>"friends"}
Run Code Online (Sandbox Code Playgroud)

我想通过简单的链接使用put来更新朋友,如下所示:
<%= link_to"Add as friend",user_friend_path(current_user,:method =>'put')%>

但是当Rails遇到这个链接时,他试图进入节目动作.
问题是:这样做的正确链接是什么?

routes ruby-on-rails put link-to

21
推荐指数
1
解决办法
2万
查看次数

link_to删除网址无法正常工作

我在我的应用程序中有以下link_to删除网址

<%=link_to "Delete",blog_path(@blog.id), :method => :delete, :class => "delete", :confirm => "Are you sure ?"%>  
Run Code Online (Sandbox Code Playgroud)

它似乎没有工作.当我点击这个网址时,它只是带我去展示路径.有人请告诉我如何解决这个问题.谢谢.

ruby-on-rails form-helpers link-to ruby-on-rails-3

20
推荐指数
3
解决办法
3万
查看次数

Rails 3 - link_to with image_tag + text

<%= link_to ((image_tag 'image.png'), 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>
Run Code Online (Sandbox Code Playgroud)

这部分代码将生成image.png作为链接.我需要这个图像附加一些文本(图像+文本),我尝试了类似的东西:

<%= link_to ((image_tag 'image.png', 'text'), 
        url_for({:controller => 'controller_name', :action => 'action_name'}), 
            :class => 'quick', 
            :remote => true) %>
Run Code Online (Sandbox Code Playgroud)

和类似的方式,但这些尝试中的每一个都以一个关于错误语法的错误消息结束......任何人都可以帮助我,请问我应该如何设置它?

提前致谢.

image link-to ruby-on-rails-3

20
推荐指数
2
解决办法
4万
查看次数

我可以使用link_to生成内部跨度的链接吗?

我基本上试图得到这个结果:

        <a href="#" class="button small-button green-button">
            Log in
            <span class="button-right"></span>
        </a>
Run Code Online (Sandbox Code Playgroud)

但我不知道如何使用rails 3中的link_to来做到这一点?

ruby-on-rails href hyperlink link-to

19
推荐指数
1
解决办法
1万
查看次数

未定义的方法'link_to'

我正在写一个ruby-on-rails库模块:

module Facets

  class Facet
    attr_accessor :name, :display_name, :category, :group, :special

    ...

    URI = {:controller => 'wiki', :action => 'plants'}
    SEARCH = {:status => WikiLink::CURRENT}

    #Parameters is an hash of {:field => "1"} values
    def render_for_search(parameters)
    result = link_to(display_name, URI.merge(parameters).merge({name => "1"}))
    count = WikiPlant.count(:conditions => (SEARCH.merge(parameters.merge({name => "1"}))))
    result << "(#{count})"
    end
  end

  ...

end
Run Code Online (Sandbox Code Playgroud)

当我调用render_for_search时,我得到了错误

undefined method 'link_to'
Run Code Online (Sandbox Code Playgroud)

我已经尝试过直接要求url_helper,但无法弄清楚出了什么问题.

ruby-on-rails link-to url-helper

18
推荐指数
3
解决办法
1万
查看次数

link_to:action =>'create'去索引而不是'create'

我正在构建一个相当简单的配方应用程序来学习RoR,我试图通过单击链接而不是通过表单来允许用户保存配方,所以我通过link_to连接user_recipe控制器的'create'功能.

不幸的是,由于某种原因,link_to正在调用索引函数而不是create.

我把link_to写成了

<%= "save this recipe", :action => 'create', :recipe_id => @recipe %>

此链接位于user_recipes/index.html.erb上,并且正在调用同一控制器的"create"功能.如果我包含:controller,它似乎没有什么区别.

控制器看起来像这样

def index
    @recipe = params[:recipe_id]
    @user_recipes = UserRecipes.all # change to find when more than one user in db
    respond_to do |format|
         format.html #index.html.erb
         format.xml { render :xml => @recipes }
    end
end

def create
    @user_recipe = UserRecipe.new
    @user_recipe.recipe_id = params[:recipe_id]
    @user_recipe.user_id = current_user
    respond_to do |format|
      if @menu_recipe.save
        format.html { redirect_to(r, :notice => 'Menu was successfully created.') }
        format.xml  { render :xml = …

ruby-on-rails link-to

18
推荐指数
2
解决办法
3万
查看次数