我正在尝试同时使用另一个嵌套资源创建一个资源.我正在使用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
好吧,这是一个很长的问题,但我认为这是值得的。我们有什么:
一个示例虚拟 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)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)具有两个公共方法的示例 WebAPI 控制器:
[RoutePrefix("sample"), …Run Code Online (Sandbox Code Playgroud)我想创建一个列表,获取属性在某个类列表中的所有值,并将这些值添加到另一个类型与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属性从或更改int为long或者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是一个变量,但它像一个类型一样使用".
有没有办法做到这一点?