伙计,我的项目中有一些奇怪的东西
现在我得到了旧的 ruby on Rails 项目,在激活命令后在其中开发一些注释
bundle
bundle exec rake db:create
bundle exec rake db:migrate
rails s
首先,应用程序要求我安装 mysql2 0.3.14,安装后出现错误
log writing faild. invalid byte sequence in US-ASCII
我通过在项目模型的每一行第一行添加 #encoding: utf-8
解决了这个问题
现在我终于可以很好地打开我的项目但是
我发现当我从我的数据库读取数据时,它工作得很好,当我尝试从我的 MYSQL 数据库插入或更新任何数据时,我什么也没发生!我以为我的数据库用户只拥有从数据库读取的权限,但在检查用户权限后,我发现它拥有所有权限
谁能向我解释一下为什么我要解决这个问题???
这是插入到我的 MYSQL 数据库中的简单代码 查看代码
<%= form_for(@category) do |f| %>
<div class="field">
<%= f.label :title %><br>
<%= f.text_field :title %>
</div>
<div class="field">
<%= f.label :icon %><br>
<%= f.file_field :icon %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Controller 代码
def create
#Just test to insert fixed data to my DB but also not insert any thing
@category =Category.new(:title => 'eeeee', :icon_file_name => 'xxx', :slug => 'sug')
#@category =Category.new(:title => params[:title], :icon_file_name => params[:icon], :slug => params[:slug])
#@category = Category.new(category_params)
respond_to do |format|
if @category.save
format.html { redirect_to @category, notice: 'Category was successfully created.' }
format.json { render action: 'show', status: :created, location: @category }
else
format.html { render action: 'new' }
format.json { render json: @category.errors, status: :unprocessable_entity }
end
end
end
注意:我使用的是 Windows 7
最佳答案
看来您没有清理您的参数。 Rails 4 中引入了强大的参数,以提高将数据发布到数据库的不受信任来源的安全性。
因此,在您的 Controller 中,您必须添加一个私有(private)方法(如果您使用脚手架生成它,它应该已经存在)来说明要接受哪些参数。
在你的情况下应该是这样的
def category_params
params.require(:category).permit(:title, :icon_file_name, :slug, :whatever_other_param_you_need)
end
在此处了解更多信息 http://edgeapi.rubyonrails.org/classes/ActionController/StrongParameters.html
关于mysql - 为什么我的 mysql DB 可以在 Rails 4 中读取而不更新或插入数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22684179/