按照我发现的教程后.我现在再次重做它,没有脚手架部分,更好地学习它.
但是,编辑我的\ app\views\home\index.html.erb以包含:
<h1>Rails test project</h1>
<%= link_to "my blog", posts_path>
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
undefined local variable or method `posts_path' for #<ActionView::Base:0x4e1d954>
Run Code Online (Sandbox Code Playgroud)
在我这样做之前,我跑了rake db:create
,定义了一个迁移类并运行rake db:migrate
,一切都没有问题.
所以数据库应该包含一个posts表.但那个link_to
命令似乎无法找到posts_path
.那个变量(或者它甚至是一个函数?)可能是通过脚手架例程定义的.
我现在的问题是; 我如何自己手动完成,定义posts_path
?
我有一个使用下面结构的rails帮助器,但是当我使用它时,我得到了消息
undefined method 'link_to'
Run Code Online (Sandbox Code Playgroud)
助手安排如下:
module MyHelper
class Facet
def render_for_search
link_to("Value", params)
end
end
class FacetList
attr_accessor :facets
def initialize
#Create facets
end
def render_for_search
result = ""
facets.each do |facet|
result << facet.render_for_search
end
result
end
end
end
Run Code Online (Sandbox Code Playgroud) 我使用link_to遇到了一个问题.为什么我的链接使用GET方法而我的button_to使用POST方法,在link_to参数中指定了我的"method"=>"post"之后?
视图:
<%= button_to "pdf", :action => 'getquote' %>
<%= link_to 'pdf', {:controller => 'inventories', :action => 'getquote', :method => :post } %>
Run Code Online (Sandbox Code Playgroud)
控制器方法:
def getquote
@cart = find_cart
respond_to do |format|
format.pdf
end
end
Run Code Online (Sandbox Code Playgroud)
终端输出(分别为按钮/链接):
Processing InventoriesController#getquote (for 127.0.0.1 at 2010-01-30 01:38:02) [POST]
Parameters: {"action"=>"getquote", "authenticity_token"=>"D2cwnHyTHgomdUM3wXBBXlOe4NQLmv1Srn0paLbExpQ=", "controller"=>"inventories"}
Processing InventoriesController#show (for 127.0.0.1 at 2010-01-30 01:39:07) [GET]
Parameters: {"method"=>"post", "action"=>"show", "id"=>"getquote", "controller"=>"inventories"}
Run Code Online (Sandbox Code Playgroud) 我有一个由Rails 3脚手架生成的基本视图设置.它给了我一个局部视图_form.html.erb.我的edit.html.erb和我的new.html.erb都渲染了这个局部视图.
在局部视图中,我希望有一个link_to转到不同的路径,具体取决于它是由新视图还是编辑视图呈现.
是否有捷径可寻?
我的代码目前看起来像这样,但不允许不同的路径.
<%= f.submit %> or <%= link_to 'Go back', models_path %>
Run Code Online (Sandbox Code Playgroud)
如果它有帮助,我试图将它们发送回他们来自的页面(它们来自不同的地方以进行添加和编辑)
在index.html.erb
我显示所有产品,并在我拥有的每个产品Edit
和Delete
行动旁边:
<% @products.each do |product| %>
...
<%= link_to("Edit", edit_product_path(product.id), :class => 'action') %>
<%= link_to("Delete", product, :method => :delete, :class => 'action') %>
...
<% end %>
Run Code Online (Sandbox Code Playgroud)
该Edit
链接的作品确定.但是,该Delete
链接不起作用.我收到以下错误:
Unknown action
The action 'show' could not be found for ProductsController
Run Code Online (Sandbox Code Playgroud)
我想这是因为请求方法是GET而不是DELETE.但是,如果我明确设置,我不知道为什么会发生这种情况:method => :delete
.
routes.rb
非常简单:
root :to => "products#index"
resources :products
Run Code Online (Sandbox Code Playgroud)
我启用了Javascript.
请建议.
是否有一种简单直接的方法可以在视图中提供链接,以便在资源不存在时创建资源,如果存在,则编辑现有资源?
IE:
User has_one :profile
Run Code Online (Sandbox Code Playgroud)
目前我会做的事......
-if current_user.profile?
= link_to 'Edit Profile', edit_profile_path(current_user.profile)
-else
= link_to 'Create Profile', new_profile_path
Run Code Online (Sandbox Code Playgroud)
这是好的,如果这是唯一的方法,但我一直在试图看看是否有一个"Rails方式"来做类似的事情:
= link_to 'Manage Profile', new_or_edit_path(current_user.profile)
Run Code Online (Sandbox Code Playgroud)
有没有什么好干净的方法来做那样的事情?类似于视图的东西Model.find_or_create_by_attribute(....)
我正在将一个项目从rails 3.1移动到rails 3.2.2并且我有这个:
= link_to 'CSV', :action => 'list', :search => @search, :format => 'csv'
Run Code Online (Sandbox Code Playgroud)
在rails 3.1中,它指定了html链接中的格式(format = csv),并且它被respond_with捕获,但在3.2.2中,格式永远不会进入链接.我扫描了github上的提交列表,找不到任何与此相关的内容.
编辑:
看起来这是url_for的问题
#rails 3.1
url_for :controller=>'posts', :action=>'index', :format=>:xml
/admin/posts/index?format=xml
#rails 3.2.2
url_for :controller=>'posts', :action=>'index', :format=>:xml
/admin/posts/index
#rails 3.2.2
url_for :controller=>'posts', :action=>'index', :format=>:xml, :id => 5
/admin/posts/index/5.xml
Run Code Online (Sandbox Code Playgroud) 我想只在语句为真时才向link_to添加一个类.
<%= link_to product.name, product, :class => "last" if product == @products.last %>
Run Code Online (Sandbox Code Playgroud)
IF语句影响整行而不仅仅是:class部分的问题.
我知道我可以用IF ELSE完成它,但它可以在一行中完成吗?
我想使用以下代码删除帖子:
<%= link_to 'Destroy', post, :method => :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
Run Code Online (Sandbox Code Playgroud)
这不起作用......它只是将我重新定向回帖子 (posts/:id}
但是,如果我使用以下代码,它的工作原理
<%= button_to 'Destroy', post, method: :delete, :onclick => "return confirm('Are you sure you want to delete this post?')" %>
Run Code Online (Sandbox Code Playgroud)
是否可以link_to
像button_to
这种情况一样表现?
编辑:破坏控制器功能
def destroy
@post = Post.find(params[:id])
@post.destroy
respond_to do |format|
format.html { redirect_to posts_url }
format.json { head :no_content }
end
end
Run Code Online (Sandbox Code Playgroud)
单击"销毁"按钮时记录:
Started GET "/posts/14" for 127.0.0.1 at 2012-10-21 15:38:28 +0300 …
Run Code Online (Sandbox Code Playgroud) 我的index.html.erb中有以下代码
<%= select_tag 'team_id', options_for_select(@teams.map{|team| ["#{team.name} #
{team.nick}", team.id] }) %>
Run Code Online (Sandbox Code Playgroud)
我会在哪个块中添加link_to帮助器?我可以为select_tag添加link_to吗?
所需的link_to将转到'/ Teamleader/ID_OF_OPTION_PICKED'
更新:
更清楚; 当用户从select标签中选择一个选项时,我想将页面重定向到所需的链接(来自link_to).
link-to ×10
class ×2
helpers ×2
edit ×1
erb ×1
forms ×1
if-statement ×1
new-operator ×1
ruby ×1
scaffolding ×1