如何使用mongoid和rails更新布尔值?

时间:2014-07-18 20:58:19

标签: ruby-on-rails-4 mongoid

我已经获得了"已发布"的初步布尔值设为' false'来自之前的帖子,但是从这个预览屏幕我想允许用户将此值更新为“真实”'点击发布这篇文章,但遇到了困难。

VIEW - preview.html.erb

<div class="preview clearfix">
        <%= form_for @job, url: '/jobs/publish/' do |f| %>
        <%= f.hidden_field :publish, :value => true %>

        <span><i class="glyphicon glyphicon-exclamation-sign"></i> Review your job post! If approved, submit it, or cancel and try again.</span>

        <%= f.submit "Post Job", :class => 'btn btn-default btn-sm' %>
        <a href="/jobs/new" class="btn" style="margin-top:3px;">Cancel</a>

        <% end %>
</div>

CONTROLLER - jobs_controller.rb

class JobsController < ApplicationController
def index
    @jobs = Job.all #where(publish: true)
end

def show
    @job = Job.find(params[:id])
end

def new
end

def preview
    @job = Job.find(params[:id])
end

def create
    job = Job.new
    job.job_title = params[:job][:job_title]
    job.job_location = params[:job][:job_location]
    job.job_description = params[:job][:job_description]
    job.job_responsibilities = params[:job][:job_responsibilities]
    job.job_requirements = params[:job][:job_requirements]
    job.specialties = params[:job][:specialties]
    job.job_level = params[:job][:job_level]
    job.how_to_apply = params[:job][:how_to_apply]
    job.company = params[:job][:company]
    job.company_description = params[:job][:company_description]
    job.company_website = params[:job][:company_website]
    job.avatar = params[:job][:avatar]
    job.agree = params[:job][:agree]
    job.publish = false
    job.save!
    redirect_to '/jobs/preview/' + job.id
end

def update
    job = Job.find(params[:id])  ## Using "find"
    job.publish = true
    job.save!
    redirect_to '/jobs'
end

end

ROUTE

  post 'jobs/publish/' => 'jobs#update'

MODEL - job.rb

class Job
include Mongoid::Document
mount_uploader :avatar, AvatarUploader

field :job_title, type: String
field :job_location, type: String
field :job_description, type: String
field :job_responsibilities, type: String
field :job_requirements, type: String
field :specialties, type: String
field :job_level, type: String

field :how_to_apply, type: String

field :company, type: String
field :company_description, type: String
field :company_website, type: String

field :agree, type: Boolean

field :publish, type: Boolean

end

日志

GridfsController#avatar处理为JPEG   参数:{&#34; id&#34; =&gt;&#34; 53c985c36d7572b6cd000000&#34;,&#34; filename&#34; =&gt;&#34; cuben-rustic&#34;}   MOPED:127.0.0.1:27017 QUERY database = ffos_development collection = jobs selector = {&#34; _id&#34; =&gt; BSON :: ObjectId(&#39; 53c985c36d7572b6cd000000&#39;)} flags = [] limit = 0 skip = 0 batch_size = nil fields = nil runtime:2.8140ms   MOPED:127.0.0.1:27017 QUERY database = ffos_development collection = fs.files selector = {&#34; $ query&#34; =&gt; {&#34; filename&#34; =&gt;&#34; uploads / job /avatar/53c985c36d7572b6cd000000/cuben-rustic.jpg"},&#34; $ orderby&#34; =&gt; {&#34; uploadDate&#34; =&gt; -1}} flags = [] limit = - 1 skip = 0 batch_size = nil fields = nil runtime:0.4260ms   MOPED:127.0.0.1:27017 COMMAND database = ffos_development command = {:count =&gt;&#34; fs.chunks&#34;,:query =&gt; {&#34; files_id&#34; =&gt; BSON :: ObjectId(&#39; 53c985c36d7572b6cd020000&#39;)}}运行时:0.3460ms   MOPED:127.0.0.1:27017 QUERY database = ffos_development collection = fs.chunks selector = {&#34; $ query&#34; =&gt; {&#34; files_id&#34; =&gt; BSON :: ObjectId(&# 39; 53c985c36d7572b6cd020000&#39;),&#34; n&#34; =&gt; {&#34; $ lt&#34; =&gt; 7,&#34; $ gte&#34; =&gt; 0}}, &#34; $ orderby&#34; =&gt; {&#34; n&#34; =&gt; 1}} flags = [] limit = 0 skip = 0 batch_size = nil fields = nil runtime:8.0450ms   MOPED:127.0.0.1:27017 GET_MORE database = ffos_development collection = fs.chunks limit = 0 cursor_id = 120760258650 runtime:1.8960ms   MOPED:127.0.0.1:27017 COMMAND database = ffos_development command = {:count =&gt;&#34; fs.chunks&#34;,:query =&gt; {&#34; files_id&#34; =&gt; BSON :: ObjectId(&#39; 53c985c36d7572b6cd020000&#39;)}}运行时:0.4970ms   渲染文本模板(0.3ms) 发送数据(0.6ms) 在23ms完成200 OK(浏览次数:0.6ms)

开始GET&#34; /uploads/job/avatar/53c98e5a6d7572b9c6000000/cuben-rustic.jpg"在2014-07-18 14:30:18 -0700为127.0.0.1 由GridfsController#avatar处理为JPEG   参数:{&#34; id&#34; =&gt;&#34; 53c98e5a6d7572b9c6000000&#34;,&#34; filename&#34; =&gt;&#34; cuben-rustic&#34;}   MOPED:127.0.0.1:27017 QUERY database = ffos_development collection = jobs selector = {&#34; _id&#34; =&gt; BSON :: ObjectId(&#39; 53c98e5a6d7572b9c6000000&#39;)} flags = [] limit = 0 skip = 0 batch_size = nil fields = nil runtime:0.4900ms   MOPED:127.0.0.1:27017 QUERY database = ffos_development collection = fs.files selector = {&#34; $ query&#34; =&gt; {&#34; filename&#34; =&gt;&#34; uploads / job /avatar/53c98e5a6d7572b9c6000000/cuben-rustic.jpg"},&#34; $ orderby&#34; =&gt; {&#34; uploadDate&#34; =&gt; -1}} flags = [] limit = - 1 skip = 0 batch_size = nil fields = nil runtime:0.5350ms   MOPED:127.0.0.1:27017 COMMAND database = ffos_development command = {:count =&gt;&#34; fs.chunks&#34;,:query =&gt; {&#34; files_id&#34; =&gt; BSON :: ObjectId(&#39; 53c98e5a6d7572b9c6020000&#39;)}}运行时:0.7430ms   MOPED:127.0.0.1:27017 QUERY database = ffos_development collection = fs.chunks selector = {&#34; $ query&#34; =&gt; {&#34; files_id&#34; =&gt; BSON :: ObjectId(&# 39; 53c98e5a6d7572b9c6020000&#39;),&#34; n&#34; =&gt; {&#34; $ lt&#34; =&gt; 7,&#34; $ gte&#34; =&gt; 0}}, &#34; $ orderby&#34; =&gt; {&#34; n&#34; =&gt; 1}} flags = [] limit = 0 skip = 0 batch_size = nil fields = nil runtime:6.5520ms   MOPED:127.0.0.1:27017 GET_MORE database = ffos_development collection = fs.chunks limit = 0 cursor_id = 120550877243 runtime:1.9530ms   MOPED:127.0.0.1:27017 COMMAND database = ffos_development command = {:count =&gt;&#34; fs.chunks&#34;,:query =&gt; {&#34; files_id&#34; =&gt; BSON :: ObjectId(&#39; 53c98e5a6d7572b9c6020000&#39;)}}运行时:0.4840ms   渲染文本模板(0.2ms) 发送数据(0.6ms) 在19ms完成200 OK(浏览次数:0.5ms)

2 个答案:

答案 0 :(得分:1)

我注意到的一个问题是您正在使用find_by方法错误地找到Job记录。如果您必须使用find_by,则必须根据您要搜索的模型指定该模型的属性名称,这在您的案例中是缺失的。

更好的选择是在您根据主键find进行搜索时使用id方法,即params[:job][:id]引用特定{{1}的id记录。

您需要做的是,替换

Job

使用

job = Job.find_by(params[:job][:id])

-OR -

job = Job.find(params[:job][:id])  ## Using "find"

答案 1 :(得分:0)

我得到了它的工作。我不得不将作业ID从预览页面再次传递给控制器​​。

<%= form_for @job, url: '/jobs/publish/'+@job.id do |f| %>

然后将控制器修改为:

def update
    job = Job.find(params[:id])
    job.publish = true
    job.save!
    redirect_to '/jobs'
end

并将路线更改为&#34; patch&#34;:

patch '/jobs/publish/:id' => 'jobs#update'