小编Rai*_*ner的帖子

如何使用Devise处理不同的用户类型

我有2种类型的用户.公司和工人.

如何在我只有1个用户的设备中进行管理?

我想为公司和工人创建2个注册页面,但我只想要1个存储用户信息的表格.

authentication rubygems ruby-on-rails devise

5
推荐指数
1
解决办法
6597
查看次数

Mechanize - 如何在rails中关注或"单击"Meta刷新

我对Mechanize有点麻烦.

当使用Mechanize提交表单时.我来到一个页面有一个元刷新,没有链接.

我的问题是我如何遵循元刷新?

我试图允许元刷新,但后来我得到一个套接字错误.示例代码

require 'mechanize'
agent = WWW::Mechanize.new
agent.get("http://euroads.dk")
form = agent.page.forms.first
form.username = "username"
form.password = "password"
form.submit
page = agent.get("http://www.euroads.dk/system/index.php?showpage=login")
agent.page.body
Run Code Online (Sandbox Code Playgroud)

响应:

<html>
 <head>
   <META HTTP-EQUIV=\"Refresh\" CONTENT=\"0;URL=index.php?showpage=m_frontpage\">
 </head>
</html>
Run Code Online (Sandbox Code Playgroud)

然后我尝试:

redirect_url = page.parser.at('META[HTTP-EQUIV=\"Refresh\"]')[
  "0;URL=index.php?showpage=m_frontpage\"][/url=(.+)/, 1]
Run Code Online (Sandbox Code Playgroud)

但我得到:

NoMethodError: Undefined method '[]' for nil:NilClass

ruby ruby-on-rails mechanize

5
推荐指数
1
解决办法
4640
查看次数

Ruby Mechanize表刮除不捕获整行

我试图用机械化刮一个表网站.我想刮第二排.

当我跑:

agent.page.search('table.ea').search('tr')[-2].search('td').map{ |n| n.text }

我希望它会刮掉整排.但相反它只是刮擦:["2011-02-17","0,00"]

为什么不抓取行中的所有列,而只是第一列和最后一列?

Xpath: / html/body/center/table/tbody/tr [2]/td [2]/table/tbody/tr [3]/td/table/tbody/tr [2]/td/table/tbody/tr [2]

CSS路径: html体中心表tbody tr td table tbody tr td table tbody tr td table.ea tbody tr td.total

该页面与此类似:

<table><table><table>
<table width="100%" border="0" cellpadding="0" cellspacing="1" class="ea">
<tr>
    <th><a href="#">Date</a></th>
    <th><a href="#">One</a></th>    
    <th><a href="#">Two</a></th>    
    <th><a href="#">Three</a></th>     
    <th><a href="#">Four</a></th>    
    <th><a href="#">Five</a></th>        
    <th><a href="#">Six</a></th>        
    <th><a href="#">Seven</a></th>      
    <th><a href="#">Eight</a></th>
</tr>
<tr>
    <td><a href="#">2011-02-17</a></td>
    <td align="right">0</td>    
    <td align="right">0</td>    
    <td align="right">0,00</td>     
    <td align="right">0</td>    
    <td align="right">0</td>        
    <td align="right">0</td>    
    <td align="right">0</td>        
    <td align="right">387</td>      
    <td …
Run Code Online (Sandbox Code Playgroud)

hpricot ruby-on-rails nokogiri ruby-on-rails-3 mechanize-ruby

5
推荐指数
1
解决办法
5726
查看次数

Rails编辑表单不显示嵌套项

我有一个有嵌套链接的表单.编辑时链接字段为空的问题.这是我的表格:

<h1>Editing kategori</h1>
<%= simple_form_for(@konkurrancer, :url => {:action => 'update', :id => @konkurrancer.id }) do |f| %>
    <%= f.simple_fields_for :link_attributes do |d| %>
    <%= d.input :link, :label => 'Tracking url', :style => 'width:500;' %>
    <% end %>
    <%= f.button :submit, :value => 'Edit konkurrence' %>
<% end %>
<%= link_to 'Show', admin_konkurrancer_path %> |
<%= link_to 'Back', admin_konkurrancer_path %>
Run Code Online (Sandbox Code Playgroud)

我的konkurrencer模型:

has_one :link
Run Code Online (Sandbox Code Playgroud)

我的链接模型:

class Link < ActiveRecord::Base
belongs_to :konkurrancer
accepts_nested_attributes_for :konkurrancer
end
Run Code Online (Sandbox Code Playgroud)

我的konkurrancer编辑动作:

  def edit
    @konkurrancer = Konkurrancer.find(params[:id])
    @konkurrancer.link_attributes.build
  end
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-3 simple-form

5
推荐指数
1
解决办法
4115
查看次数

Rails如何创建Jquery flash消息而不是默认的rails消息

我想在页面顶部创建一个自定义Jquery消息而不是标准的rails flash消息.我想在我的投票底部附近创建一条flash消息.

我的控制器:

def vote_up
  @post = Post.find(params[:id])
  current_user.up_vote(@post)
  flash[:message] = 'Thanks for voting!'
  redirect_to(root_path, :notice => 'Tak for dit indlæg, det er nu online!')
rescue MakeVoteable::Exceptions::AlreadyVotedError
  flash[:error] = 'Already voted!'
  redirect_to root_path
end
Run Code Online (Sandbox Code Playgroud)

我的看法:

<% @posts.each do |post| %>
  <h1><%= post.titel %></h1>
  <p><%= post.body_html %></p>
  <p><%= link_to 'Vote up', stem_op_path(post.id) %> BRUGERNAVN: <%= post.user.username %> | UPVOTES: <%= post.up_votes %> | DOWN VOTES: <%= post.down_votes %> OPRETTET: <%= post.created_at.strftime("%d %B") %><%= link_to 'Destroy', post, :confirm => 'Are you sure?', …
Run Code Online (Sandbox Code Playgroud)

ruby jquery ruby-on-rails ruby-on-rails-3

5
推荐指数
1
解决办法
6769
查看次数

Rails 3 I18:缺少翻译:da.datetime.distance_in_words.about_x_hours

我在视图中收到此错误:

translation missing:
da.datetime.distance_in_words.about_x_hours     
Run Code Online (Sandbox Code Playgroud)

我的da语言环境文件:http://pastie.org/2944890

我的看法:

<%= distance_of_time_in_words(Time.new, konkurrancer.udtraekkes) %>
Run Code Online (Sandbox Code Playgroud)

我已将此添加到我的application.rb:

config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
config.i18n.default_locale = :da
Run Code Online (Sandbox Code Playgroud)

如果我删除I18配置,帮助程序确实可以用于英语.

更新:

我在config/enviorments/devolpment.rb中的配置:

  config.i18n.load_path += Dir[Rails.root.join('locales', '*.{rb,yml}').to_s]
  config.i18n.default_locale = :da
  config.i18n.locale = :da
Run Code Online (Sandbox Code Playgroud)

我在config/locales/da.yml中的翻译文件:

da:
   datetime:
      distance_in_words:
         x_days:
            one: '1 day'
            other: '{{count}} dage'
Run Code Online (Sandbox Code Playgroud)

我得到这个错误视图:

translation missing:  da.datetime.distance_in_words.x_days
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails internationalization ruby-on-rails-3

5
推荐指数
1
解决办法
6002
查看次数

Heroku如何只推动变化?

很多时候我只对一页做了一点改动.

我怎样才能推送更改而不git push heroku master需要花费大量时间上传.

heroku

5
推荐指数
1
解决办法
6006
查看次数

如何只清除文件上传字段?

这是JSfiddle:http://jsfiddle.net/buyC9/128/

我想清除时只清除文件上传字段.

我的HTML:

<h1>Upload image:</h1>
<input type="file" name="blog[opload]" id="blog_opload" class="imagec">
<input type="url" size="50" name="blog[url]" id="blog_url" class="imagec">
<div id="preview">
</div>
Run Code Online (Sandbox Code Playgroud)

我的Jquery:

$('input').change(function() {
    var el = $(this);
    if (this.value === "") {
        $('.imagec').prop('disabled', false);
        this.disabled = false;
        $('#preview').hide();
    } else {
        $('.imagec').prop('disabled', true);
        el.prop('disabled', false);
        alert(el.val());
        if (el.attr('type') == 'url') {
            $('#preview').show().html('<img src=' + '"' + el.val() + '"' + ' />');
        }
    }
});

function reset_html(id) {
    $('.' + id).html( $('.' + id).html() );
}

var imagec_index …
Run Code Online (Sandbox Code Playgroud)

javascript jquery

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

Ruby/Rails方法在x字符或单词后面的字符串中插入东西

哪个方法具有在x个字符后插入内容的功能.

例如,我有这个字符串:

@test = "Something, is wrong"
Run Code Online (Sandbox Code Playgroud)

然后就可以做类似的事情:

@test.insert(x words or x characters , '<br />')
Run Code Online (Sandbox Code Playgroud)

我需要这个,因为我需要<br />在一个需要分解的长字符串中插入一个换行符 .

ruby ruby-on-rails-3 ruby-on-rails-3.2

4
推荐指数
1
解决办法
6429
查看次数

Ruby/Rails Net::HTTP.post_form 嵌套哈希项 html 转义

示例我在 rails 控制台中执行此操作:

params = {"type"=>["raka"], "fields"=>["exhb_0", "exh0_1", "t_g_a", "hp_1", "s1", "overflade_0", "t2", "t3", "t4"], "railing"=>["A-3"], "wood"=>["wood_6"], "railing_m"=>"0", "order"=>{"sving"=>"right", "size"=>{"ground"=>"123", "floor"=>"6", "a"=>"6", "d"=>"6"}, "comments"=>{"step_2"=>"", "step_3"=>"", "step_4"=>""}, "railing"=>{"1"=>"1", "2"=>"1"}, "railing_m"=>{"1"=>"", "2"=>"", "3"=>"", "4"=>"12"}, "hul"=>{"l"=>"123", "b"=>"123"}, "name"=>"qwed", "email"=>"mail@example.com", "phone"=>"13123", "street"=>"iuuj", "city"=>"ui", "postnr"=>"213"}}

x = Net::HTTP.post_form(URI.parse('http://localhost:3000/download.pdf'), params)
Run Code Online (Sandbox Code Playgroud)

在我的 Rails 控制台中,我可以看到 HTTP 发布请求:

Started POST "/download.pdf" for 127.0.0.1 at 2013-04-15 16:25:36 +0200
Processing by PublicController#show_pdf as */*
  Parameters: {"type"=>"raka", "fields"=>"t4", "railing"=>"A-3", "wood"=>"wood_6
", "railing_m"=>"0", "order"=>"{\"sving\"=>\"right\", \"size\"=>{\"ground\"=>\"1
23\", \"floor\"=>\"6\", \"a\"=>\"6\", \"d\"=>\"6\"}, \"comments\"=>{\"step_2\"=>
\"\", \"step_3\"=>\"\", \"step_4\"=>\"\"}, …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails ruby-on-rails-3

4
推荐指数
1
解决办法
2208
查看次数