我在理解changeset
模型时遇到问题.它能做什么?我们可以在一个模型中拥有多个变更集吗?例如一个用于创建,另一个用于更新.
有人可以用简单的方式详细说明,这样可以帮助其他人来凤凰城.
我如何为我的模型编写表单,我使用rails 4和https://github.com/globalize/globalize进行翻译.我想在一个表单中显示所有翻译,如下例所示.我在这里找到了一个解决方案https://github.com/rilla/batch_translations但我不知道如何实现这一点.这个"批量翻译"是一个宝石还是什么?以及如何安装它.
<h1>Editing post</h1>
<% form_for(@post) do |f| %>
<%= f.error_messages %>
<h2>English (default locale)</h2>
<p><%= f.text_field :title %></p>
<p><%= f.text_field :teaser %></p>
<p><%= f.text_field :body %></p>
<hr/>
<h2>Spanish translation</h2>
<% f.globalize_fields_for :es do |g| %>
<p><%= g.text_field :title %></p>
<p><%= g.text_field :teaser %></p>
<p><%= g.text_field :body %></p>
<% end %>
<hr/>
<h2>French translation</h2>
<% f.globalize_fields_for :fr do |g| %>
<p><%= g.text_field :title %></p>
<p><%= g.text_field :teaser %></p>
<p><%= g.text_field :body %></p>
<% end %>
<% …
Run Code Online (Sandbox Code Playgroud) 我希望获得所有主题名称与搜索关键字匹配的个人资料.现在我正在加载所有配置文件.我需要知道如何实现它.任何帮助深表感谢.
Profile.rb
has_many :categorizations
has_many :subjects, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
subject.rb中
has_many :categorizations
has_many :profiles, through: :categorizations
Run Code Online (Sandbox Code Playgroud)
Categorization.rb
belongs_to :profile
belongs_to :subject
Run Code Online (Sandbox Code Playgroud)
意见/搜索/ index.html.erb
# search form
<%= form_tag search_index_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "search", :name => nil %>
<% end %>
# search results
<% @profiles.each do |profile| %>
<%= profile.name %>
<% end %>
Run Code Online (Sandbox Code Playgroud)
search_controller.rb
def index
@profiles = Profile.with_translations('en').all
end
Run Code Online (Sandbox Code Playgroud) 如何使用太阳黑子solr索引阿拉伯语配置文件翻译.我可以使用全球化和太阳黑子,还是应该使用其他方法?
车型/ profile.rb
translates :name, :description
validates :name
validates :description
searchable do
text :name
text :description
end
Run Code Online (Sandbox Code Playgroud) ruby ruby-on-rails internationalization sunspot ruby-on-rails-4
我是elixir和phoenix的新手,并且使用凤凰指南.在入门指南中他们只是说http://puu.sh/klblD/e50082298d.png,但他们没有解释什么是端点,什么是任务.有人可以解释它有什么作用吗?
我为用户资源创建身份验证,它工作正常,但现在我想使用user_controller.ex中的authenticate函数到project_controller.ex.
如果我将私有函数从user_controller复制到project_controller,而不是身份验证适用于项目资源,但我不想在每个控制器中复制此验证函数.我需要知道干这个代码的最佳方法是什么.我认为路由器是添加身份验证插件的好地方,但我需要知道我应该在哪里添加代码.
defmodule Auth.Router do
use Auth.Web, :router
pipeline :browser do
plug :accepts, ["html"]
plug :fetch_session
plug :fetch_flash
plug :protect_from_forgery
plug :put_secure_browser_headers
plug Auth.Auth, repo: Auth.Repo
end
pipeline :api do
plug :accepts, ["json"]
end
scope "/", Auth do
pipe_through :browser # Use the default browser stack
get "/", PageController, :index
resources "/users", UserController
resources "/sessions", SessionController, only: [:new, :create, :delete]
resources "/projects", ProjectController
end
end
Run Code Online (Sandbox Code Playgroud)
defmodule Auth.UserController do
use Auth.Web, :controller
plug :authenticate when action in [:index, :show] …
Run Code Online (Sandbox Code Playgroud) 我有以下型号......
Page
Category
我已经在下面的代码new
的作用page_controller.ex
def new(conn, _params) do
changeset = Page.changeset(%Page{})
categories = Repo.all(Category)
render(conn, "new.html", changeset: changeset, categories: categories)
end
Run Code Online (Sandbox Code Playgroud)
我在page/new.html.eex中有关于select字段的以下代码
<div class="form-group">
<%= label f, :category_id, "Parent", class: "control-label" %>
<%= select f, :category_id, @categories ,class: "form-control" %>
</div>
Run Code Online (Sandbox Code Playgroud)
它应该在选择字段中显示所有类别,以便我可以为页面选择一个类别,但不幸的是我无法找到问题.如果您有任何建议,请告诉我.
细绳: Lorem ipsum {{dolor}} sit {{amet}}, consectetur adipisicing {{elit}},
我想从上面提到的字符串中提取包含在 {{ }} 中的所有标签的列表并对其进行处理,即检查数据库是否存在,然后将它们替换为降价链接,如下所示。
[substring](/tag/:substring_id)
Run Code Online (Sandbox Code Playgroud)
我可以将它们替换为
String.replace(string, ~r/\{\{.+?\}\}/, "new substring")
Run Code Online (Sandbox Code Playgroud)
但这对我没有帮助,因为我无法处理子字符串,即检查数据库。
我没有找到任何将子字符串作为列表返回的 String.scan 或 String.find 类型的函数。如果您知道该怎么做,请告诉我。
预先感谢您的努力和时间:)
我想看看凤凰1.3-dev
.我从存储库克隆凤凰,然后按照phoenix/installer/README.md
构建存档.
phoenix/installer/README.md(供参考)
$ cd installer
$ MIX_ENV=prod mix archive.build
$ mix archive.install
Run Code Online (Sandbox Code Playgroud)
我创建了一个新的凤凰应用程序mix phx.new blog
但是当我试图创建一个简单的html脚手架时,mix phx.gen.html Post posts title body:text
我得到以下错误消息:
** (Mix) Expected the schema argument, ":\"Elixir.Company.companies\"", to be a valid module name
mix phx.gen.html and mix phx.gen.json expect a context module name,
followed by singular and plural names of the generated resource, ending
with any number of attributes:
mix phx.gen.html Accounts User users name:string
mix phx.gen.json Accounts User users …
Run Code Online (Sandbox Code Playgroud)