小编vas*_*sib的帖子

我应该如何将rails和simple_form用于嵌套资源?

我正在尝试同时使用另一个嵌套资源创建一个资源.我正在使用Rails4和simple_form 3.0.0rc.这是我的代码.

楷模:

class User < ActiveRecord::Base
  has_one :profile
  accepts_nested_attributes_for :profile
end

class Profile < ActiveRecord::Base
  belongs_to :user
end
Run Code Online (Sandbox Code Playgroud)

控制器:

class UsersController < ApplicationController
  def new
    @user = User.new
    @user.build_profile
  end

  def create
    user = User.new user_params
    user.save
    redirect_to root_url
#    @par =params
  end

  private
    def user_params
      params.require(:user).permit(:email, profile_attributes: [:name])
    end
end
Run Code Online (Sandbox Code Playgroud)

查看(新用户的表单)

<%= simple_form_for @user do |f| %>
  <%= f.input :email %>
  <%= simple_fields_for :profile do |p| %>
    <%= p.input :name %>
  <% end %>
  <%= f.submit %>
<% …
Run Code Online (Sandbox Code Playgroud)

ruby-on-rails nested-attributes fields-for simple-form strong-parameters

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

C# HttpListener 多种身份验证方案和 Chrome

好吧,这是一个很长的问题,但我认为这是值得的。我们有什么:

  1. 一个示例虚拟 C# 控制台应用程序,启动自托管 ASP.Net WebAPI 服务(Microsoft.AspNet.WebApi.OwinSelfHostNuGet 包):

    class Program
    {
        static void Main(string[] args)
        {
            var url = "http://localhost:8080/";
    
            Console.WriteLine($"Starting on {url}");
            using (WebApp.Start<Startup>(url))
            {
                Console.WriteLine("Success! Press any key to stop...");
                Console.ReadKey();
            }
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. OWIN启动类:

    class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            // enable Windows AND Anonymous authentication
            var listener = app.Properties["System.Net.HttpListener"] as HttpListener;
            listener.AuthenticationSchemes = AuthenticationSchemes.Anonymous |
                AuthenticationSchemes.IntegratedWindowsAuthentication;
    
            // configure WebAPI
            var config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();
    
            app.UseWebApi(config);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 具有两个公共方法的示例 WebAPI 控制器:

    [RoutePrefix("sample"), …
    Run Code Online (Sandbox Code Playgroud)

c# google-chrome http httplistener www-authenticate

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

创建与类属性相同类型的List

我想创建一个列表,获取属性在某个类列表中的所有值,并将这些值添加到另一个类型与class属性相同的列表中.

让我们假设我有Book类:

public class Book{
    public int code;
    public string genre;
    public string author;
    public string synopsis;
}
Run Code Online (Sandbox Code Playgroud)

然后,我有一些List<Book> myList包含一些书籍的方法,我想填充一个bookCodes列表,它应该与Book的属性代码类型相同.List<int> myBookCodes如果有一天code属性从或更改intlong或者Guid,我想不要更改BookCodes列表的声明.

我尝试过诸如此类的东西

var myBookCodes = new List<typeof(Books.code)>
Run Code Online (Sandbox Code Playgroud)

但我收到"预期类型"错误.

我也试过使用反射,但我也有错误:

System.Reflection.PropertyInfo bookCodeProperty = typeof(Book).GetProperty("code");
Type bookCodeType = bookCodeProperty.PropertyType;

var myBookCodes = new List <>(bookCodeType)
Run Code Online (Sandbox Code Playgroud)

但在这种情况下,编译器会抱怨"bookCodeType是一个变量,但它像一个类型一样使用".

有没有办法做到这一点?

c# generics list

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