如何在用户模型中保存静态页面属性 [英] How to save static page attributes in User model
问题描述
要求:我需要在ROR中创建一个单页面应用程序,用户可以在其中更改页面的背景颜色,字体更改应该与用户会话持续存在。
Requirement: I need to create a single page application in ROR where user can change the background color of page, font and that change should persist with user sessions.
我正在做的是:我已经创建了一个控件static_pages,以home为索引,并在其视图下创建了一个名为home.html.erb的表单。
What I am doing is: I have created a controller static_pages with home as index and created a form under its view that named as home.html.erb.
<%= form_for @user, :url => url_for(:controller => 'static_pages', :action => 'home') do |u| %>
<br >
<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="bgcolor" id="bgcolor">
<option value="#FF3300">Orange</option>
<option value="#00FF00">Green</option>
<option value="#0000FF">Blue</option>
<option value="#FF0066">Pink</option>
<option value="#FFFF00">Yellow</option>
<option value="#FFFFFF">White</option>
</select>
</p>
<p>
<%= u.label :font %><br>
<select name="font" id="font">
<option value="Times New Roman">Times new Roman</option>
<option value="Verdana">Verdana</option>
<option value="Arial">Arial</option>
<option value="sans-serif">serif</option>
</select>
</p>
<br >
<p>
<%= u.submit %>
</p>
<hr >
<div style="background-color:#{current_user.font.nil? ? '#FFFFFF' : current_user.font}">
This is the changes made in background
</div>
<div style="background-color:#{current_user.bgcolor.nil? ? '#FFFFFF' : current_user.bgcolor}">
</div>
<% end %>
我已经为当前登录应用程序的用户创建了此表单,我已经使用devise gem现在如果用户填写此表单并提交它必须更改页面的背景颜色,我正在尝试将其保存在用户数据库中。用户的模式如下:
I have created this form for user who currently logged in the application and I have used devise gem for this.Now if user fill this form and submit it has to change the background color of page and I am trying to save this in user database. The schema for User is as:
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.text "title"
t.string "bgcolor"
t.text "description"
t.text "font"
end
我正在处理控制器中的这个数据为static_pages_controller.rb
and I am handling this data in controller as static_pages_controller.rb
class StaticPagesController < ApplicationController
def home
@user = User.new(params.require(:user).permit(:title, :description, :font, :bgcolor))
if @user.save
redirect_to '/static_pages/home'
else
flash[:notice] = "Developers to be blamed !!"
end
end
end
当我提交此表单我收到错误如下:
When I am submitting this form I am getting the error as below:
param is missing or the value is empty: user
注意:routes.rb看起来像这样
Notes: routes.rb looks like this
devise_for :users, controllers: { registrations: "users/registrations" }
devise_scope :user do
authenticated :user do
root :to => 'static_pages#home', as: :authenticated_root
end
unauthenticated :user do
root :to => 'devise/registrations#new', as: :unauthenticated_root
end
end
Please guide where I am doing mistake !!! and thanks in advance
推荐答案
尝试重定向到 root_path
而不是'/ static_pages / home'
尽管如此,您可能会遇到另一个问题: @user .save
将尝试保存新用户,而不是更新现有的用户。
You'll probably have a different problem though: @user.save
will try to save a new user instead of updating the existing one.
还要考虑隐式呈现视图,而不是显式地重定向回相同的路径。这样,整个if / else块可以减少到: flash [:notice] =开发者被指责!!除非@ user.save
Also consider implicitly rendering the view instead of explicitly redirecting back to the same path. That way, the entire if/else block can be reduced to: flash[:notice] = "Developers to be blamed !!" unless @user.save
另请注意:单页应用程序大多在客户端上呈现。控制器仍应根据REST原则进行设计。
Further note: Single page applications are mostly rendered on the client. The controllers should still be designed according to REST principles.
这篇关于如何在用户模型中保存静态页面属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!