问题描述
我需要使用ROR创建一个单页网站,允许用户修改该页面的样式。至少,用户应该能够更改页面上的颜色,标题和字体,但不能修改用于设置页面的控件。数据应该通过ActiveRecord保存,更改应该保留会话。
我的问题:我在这里创建了用户表单,并创建了用户表内的三个字段,即字体,背景颜色和标题和使用设计的身份验证,但是如何保存用户的字体,标题和样式,以便更改应反映在页面中,更改应该持续存在。
class User< ActiveRecord :: Base
devise:database_authenticatable,:可注册,
:可恢复,可记忆,可跟踪,可验证
结束
static_page.html.erb
<%= form_for:user做| u | %GT;
< p>
<%= u.label:title%>< br>
<%= u.text_field:title%>
< / p>
< p>
<%= u.label:description%>< br>
<%= u.text_field:description%>
< / p>
< p> <%= u.label:back_ground_color%>< br>
< select name =back-groundid =bgcolor>
< option value =1> Orange< / option>
< option value =2> Green< / option>
< option value =3> Blue< / option>
< option value =4> Pink< / option>
< option value =5>黄色< / option>
< option value =6> White< / option>
< / select>
< / p>
< p>
<%= u.label:font%>< br>
< select name =fontid =font>
< option value =1> Times new Roman< / option>
< option value =2> Calibri< / option>
< option value =3>幻想< / option>
< option value =4> serif< / option>
< / select>
< / p>
< br>
< p>
<%= u.submit%>
< / p>
<%end%>
现在我该怎么做提交此表单以使其工作
不要忘记 - 我需要更改背景颜色,字体,标题以及存储在数据库中的持久性。
您需要在数据库中存储颜色的十六进制值(例如:#FFFFFF)。那么在这个视图中,你可以给出内联的CSS,或者你可以使用帮助器来生成它:
content_tag ,,style =>background-color:#{current_user.font_color})
您可以以相同的方式使用inline css。以同样的方式,你可以做字体和其他东西。像这样存储字体名称:Times New Roman
,然后在 font-family
中指定。使用这些动态名称无处不在,如果有人没有输入任何值,则可以随时使用默认值:
content_tag (:span,,style =>background-color:#{current_user.font_color.nil??'#FFFFFF':current_user.font_color})
pre>
希望这有帮助。
I need to create a single page website using ROR which allows the users to modify the styling of that page. At a minimum, the user should be able to change the colors, title, and font on the page, but should not be able to modify the controls for styling the page. Data should be saved via ActiveRecord and changes should persist sessions.
My question: I have created here form for user and created three field inside user table i.e font, background color and title and using devise for authentication, but then how can I save font, title and style for users so that the changes should reflect in pages and the changes should persist with session.
class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable end
static_page.html.erb
<%= form_for :user do |u| %> <p> <%= u.label :title %><br> <%= u.text_field :title %> </p> <p> <%= u.label :description %><br> <%= u.text_field :description %> </p> <p> <%= u.label :back_ground_color %><br> <select name="back-ground" id="bgcolor"> <option value="1">Orange</option> <option value="2">Green</option> <option value="3">Blue</option> <option value="4">Pink</option> <option value="5">Yellow</option> <option value="6">White</option> </select> </p> <p> <%= u.label :font %><br> <select name="font" id="font"> <option value="1">Times new Roman</option> <option value="2">Calibri</option> <option value="3">fantasy</option> <option value="4">serif</option> </select> </p> <br > <p> <%= u.submit %> </p> <% end %>
Now what should I do to submit this form so that it should workDon't forget - I need to change the background color, font, title and as well as stored in database for persistence.
解决方案You need to store the color's hexadecimal value (eg: #FFFFFF) in the database. Then in the view either you can give inline css or you can use the helper to generate it like this:
content_tag(:span, "", :style => "background-color:#{current_user.font_color}")
You can use the inline css in the same way. And in the same way you can do it for font and other things. Like storing the font name like this:
"Times New Roman"
and then specifying it in thefont-family
. Use these dynamic names everywhere and you can always have a default value if someone doesn't enters any value then like this:content_tag(:span, "", :style => "background-color:#{current_user.font_color.nil? ? '#FFFFFF' : current_user.font_color}")
Hope this helps.
这篇关于用户如何在rails应用程序中修改颜色,页面和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!