我正在使用Kaminari的分页.它的工作非常棒.
我现在想要的一件事是,加上#comment_sectionKaminari产生的网址.
例如,我的观点就是这样.通过单击Kaminari生成的链接加载页面时,我希望它转到本节的顶部.
可能吗?
<a name="comment_section">
<span id="comment">
<%= render 'users/comment' %>
</span>
<%= paginate @comments, :window => 4 %>
Run Code Online (Sandbox Code Playgroud) 我通过位于http://ruby.railstutorial.org的教程在轨道上尝试ruby .我到了可以创建用户的位置,并显示了他们的名字和gravatar:
http://localhost:3000/users/1
Run Code Online (Sandbox Code Playgroud)
现在我想在用户访问时显示所有用户:
http://localhost:3000/users/
Run Code Online (Sandbox Code Playgroud)
这是我的控制器:
class UsersController < ApplicationController
def index
@user = User.all
end
#...
end
Run Code Online (Sandbox Code Playgroud)
这是我的看法.
#View for index action in user's controleer
<h1>All users</h1>
<ul class="users">
<% @users.each do |user| %>
<li><%= user.content %></li>
<% end %>
</ul>
Run Code Online (Sandbox Code Playgroud)
我收到以下错误.
undefined method `each' for nil:NilClass
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我为什么索引页面不能正常工作.
基本上,我在我的模型上定义了一个属性,它根据另一个表中的值返回true或false.
我想要的是让控制器中的索引操作仅返回满足此条件的结果.
我试过这个:
#What I've tried on my Controller:
def index
@projects = Project.where(:is_available?)
end
#What I've on my Controller:
def index
@projects = Project.all
end
#What I've on my Model:
def is_available?
workers.count<2 ? true : false
end
Run Code Online (Sandbox Code Playgroud)
谢谢.
如果我不是描述性的话,这里的第一个问题请原谅我.运行Ruby 1.8.7 w/rails 2.3.5如果我运行任何rails或rake任务我得到
/home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in `setup': undefined method `name' for #<Array:0x7f45a2fe73a8> (NoMethodError)
from /home/mathew/.rvm/rubies/ruby-1.8.7-p374/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `map'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in `each'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in `map'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:75:in `setup'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:114:in `check'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems-bundler/noexec.rb:121
from /home/mathew/.rvm/rubies/ruby-1.8.7-p374/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `gem_original_require'
from /home/mathew/.rvm/rubies/ruby-1.8.7-p374/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:36:in `require'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/rubygems-bundler-1.4.2/lib/rubygems_executable_plugin.rb:4
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/executable-hooks-1.2.6/lib/executable-hooks/hooks.rb:50:in `call'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/executable-hooks-1.2.6/lib/executable-hooks/hooks.rb:50:in `run'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/executable-hooks-1.2.6/lib/executable-hooks/hooks.rb:49:in `each'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@global/gems/executable-hooks-1.2.6/lib/executable-hooks/hooks.rb:49:in `run'
from /home/mathew/.rvm/gems/ruby-1.8.7-p374@wholesale/bin/ruby_executable_hooks:10
Run Code Online (Sandbox Code Playgroud)
我的宝石清单是:
*** LOCAL GEMS ***
actionmailer (2.3.5)
actionpack (2.3.5)
activerecord (2.3.5)
activeresource (2.3.5)
activesupport (2.3.5)
atomic (1.1.14)
bcrypt-ruby (3.1.2)
bundler (1.3.5)
bundler-unload (1.0.2)
daemons (1.0.10)
delayed_job (2.0.4)
executable-hooks (1.2.6) …Run Code Online (Sandbox Code Playgroud) 我正在阅读"Rails 3 Way"和第39页,它显示了匹配:to =>重定向方法的代码示例.在该方法中,存在以下代码.虽然我知道模数对数字有什么作用,但我不确定%在下面做了什么,因为路径和参数都显然不是数字.如果有人能帮我理解在这种情况下使用%,我会很感激.
proc { |params| path % params }
Run Code Online (Sandbox Code Playgroud) 登录后,我让我的SessionsController运行after_action来重置某些用户列.在另一个控制器中,我想在创建和更新时执行相同的after_action.
将相同的代码复制并粘贴到两个控制器中都很容易,但我知道有一天我会在一个控制器中进行更改而忘记更新另一个控制器.
有没有办法从不同的控制器调用与after_action相同的方法?
我有交货问题.
我得到了这个错误:
NoMethodError in PagesController#send_message
undefined method `delivery_handler=' for #< Message:0x00000004db9648>
app/mailers/contact.rb:22:in `send_email'
app/controllers/pages_controller.rb:65:in `send_message'
Run Code Online (Sandbox Code Playgroud)
无论如何,这是我的代码:
/views/pages/contact.html.erb
<h1><%= @page.title %></h1>
<%= @page.content.html_safe %>
<%= form_for @message, :url => "/pages/send_message" do |f| %>
<% if flash[:notice] %>
<% flash[:notice].full_messages.each do |msg| %>
<p><%= msg %></p>
<% end %>
<% end %>
<div class="field">
<%= f.label :email %>
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :body %>
<%= f.text_area :body %>
</div>
<%= f.submit "Envoyer" %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
/models/message.rb …
我是Ruby和Rails的新手.我在为我的服务构建json响应时遇到问题.我有两个型号:
class Transaction < ActiveRecord::Base
has_and_belongs_to_many :users
end
class User < ActiveRecord::Base
has_and_belongs_to_many :transactions
end
Run Code Online (Sandbox Code Playgroud)
因此用户拥有交易集合并且交易具有用户集合.我要做的是返回所有涉及用户参与的交易以及涉及所有交易的其他用户.
我的json应该是这样的:
{"transactions":[{"name":"transaction1", "users":[{"userName":"user1"},{"userName":"user2"}]},{"name":"transaction2", "users":[{"userName":"user1"},{"userName":"user2"}]}]}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
@user = User.find_by_facebookId(params[:id])
#json = User.find_by_facebookId(params[:id], :include => :transactions).to_json( :include => :transactions )
if @user != nil
render json: {"transactions" => @user.transactions.map do |transaction|
transaction
end
}
else
render json: {"transactions" => [{}]}
end
Run Code Online (Sandbox Code Playgroud) 在rails中你可以这样做:
<% name = @user.name %>
<script>
var name = <%= name%>;
alert(name);
//prints the name of the user
</script>
Run Code Online (Sandbox Code Playgroud)
你可以反过来这样做
<% name = @user.name %>
<script>
var name = "John";
<% name %> = name;
</script>
<% @user.name = name %>
<%= name %>
<!-- prints the name of the user, in this case "John" -->
Run Code Online (Sandbox Code Playgroud)
显然这不起作用,但是出于示例的目的
关键是,你能否将javascript变量的值传递给rails中的ruby变量
我想使用特定范围的值从哈希表中删除数据.
例:
hash = { t:1, y:9, k:10, a:30, b:40, c:50, d:80, e:60, z:100, l:3, n:9, f:20 }
给定一组数字:( array = [10, 30, 40, 50, 80, 60, 100]正好是表格中心的范围)
我希望结果如下:
hash: {k:10, a:30, b:40, c:50, d:80, e:60, z:100}
从未消除位于结构中间的数据的注释.
ruby ×5
associations ×1
bcrypt ×1
bundler ×1
callback ×1
controllers ×1
hash ×1
hashtable ×1
javascript ×1
json ×1
kaminari ×1
variables ×1