小编Chi*_*ris的帖子

Rails Generator传递一组参数

我已经看过关于如何创建自己的生成器的rails演员,并且已经阅读了许多堆栈溢出问题.基本上我要做的是构建一个像内置的rails迁移生成器一样的生成器,它接受类似的参数attribute:type然后根据需要插入它们.这是我现在为我的发电机编写的代码.

class FormGenerator < Rails::Generators::NamedBase
  source_root File.expand_path('../templates', __FILE__)
  argument :model_name, :type => :string
  argument :attributes, type: :array, default: [], banner: "attribute:input_type attribute:input_type"
  argument :input_types, type: :array, default: [], banner: "attribute:input_type attribute:input_type"



  def create_form_file
    create_file "app/views/#{model_name.pluralize}/_form.html.erb", "


<%= simple_form_for @#{model_name.pluralize} do | f | %>
<%= f.#{input_type}  :#{attribute}, label: '#{attribute}' %>
<% end %>
"
  end
end
Run Code Online (Sandbox Code Playgroud)

基本上我想要它做的是生成一个视图文件,其中包含与参数一样多的行.因此传递rails g form products name:input amount:input other_attribute:check_box会生成一个_form.html.erb包含以下内容的文件:

<%= simple_form_for @products do | f | %>
    <%= f.input  :name, …
Run Code Online (Sandbox Code Playgroud)

ruby code-generation ruby-on-rails simple-form

7
推荐指数
1
解决办法
707
查看次数

Rails Passenger Glyphicon CORS Cloudfront NGINX问题

所以我知道stackoverflow是关于CORS Nginx,Cloudfront和Heroku的这些问题,但由于某些原因我不能让它工作.我一直在关注这个问题的答案:

如何使用rails,nginx和passenger配置`Access-Control-Allow-Origin`?

但是,我似乎无法弄清楚自定义代码块的放置位置:

config/nginx.conf.erb
Run Code Online (Sandbox Code Playgroud)

接下来,通过查找如下所示的块来编辑配置文件config/nginx.conf.erb:

location @static_asset {
    gzip_static on;
    expires max;
    add_header Cache-Control public;
    add_header ETag "";
} ...and add the two Access-Control lines:
Run Code Online (Sandbox Code Playgroud)

>     location @static_asset {
>         gzip_static on;
>         expires max;
>         add_header Cache-Control public;
>         add_header ETag "";
>         add_header Access-Control-Allow-Origin *;
>         add_header Access-Control-Request-Method *;
>     } That's it. This will work in production, but not in development, due to config.assets differences between the two.
Run Code Online (Sandbox Code Playgroud)

在我的Nginx配置中.我正在使用Phusion Passenger 5.0.23.Nginx配置在我从乘客那里取出的那个中没有位置@static_asset块.它确实有一个自定义配置部分,但这对我没用.我的glyphicons继续显示为盒子我如何让它工作?我也试过这个 Phusion Passenger + Heroku …

ruby-on-rails passenger heroku cors amazon-cloudfront

6
推荐指数
1
解决办法
377
查看次数

如何使用最小控制器:使用Paperclip验证器创建操作

基本上我的:创建动作测试保持失败,即使它在应用程序中工作.我在下面的控制器中注释了回形针验证并且它有效.

  has_attached_file :image, styles: { medium: "700x700>", small: "350x250#" }
  validates_attachment_presence :image
  validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
Run Code Online (Sandbox Code Playgroud)

这是我正在运行的测试,它在注释验证时有效.我怎样才能通过符合我模型中回形针验证的参数.

test "should create if signed_in" do
      sign_in(@user)
      assert_difference 'Product.count' do
        post :create, product:{ title:'test_product', description: 'khara', user_id: @user.id}
      end
      assert_redirected_to product_path(assigns(:product))
    end
Run Code Online (Sandbox Code Playgroud)

失败的消息:

    FAIL["test_should_post_create_if_signed_in", ProductsControllerTest, 0.58458]
         test_should_post_create_if_signed_in#ProductsControllerTest (0.58s)
                "Product.count" didn't change by 1.
                Expected: 3
                  Actual: 2
                test/controllers/products_controller_test.rb:52:in `block in <class:ProductsControllerTest>'
Run Code Online (Sandbox Code Playgroud)

基本上我该怎么做这个测试通过?

注意:我知道回形针提供了测试指令,而Shoulda和Spec希望纯粹在Minitest中做到这一点.

model-view-controller ruby-on-rails paperclip minitest

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