简体   繁体   English

控制器操作中的Rails3条件语句

[英]Rails3 Conditional Statements in Controller Actions

I'm trying to write a conditional statement in my tasks controllers for a simple rails3 application. 我正在尝试在我的任务控制器中为简单的rails3应用程序编写一个条件语句。

Users have many tasks and tasks have one user. 用户有许多任务,任务有一个用户。

When creating a task, we can chose who owns it: 创建任务时,我们可以选择谁拥有它:

<%= collection_select(:task, :user_id, User.all, :id, :name, {:prompt => true}) %>

I want the system to send an email to the owner of the task, only when it's created for someone else. 我希望系统只有在为其他人创建电子邮件时才向该任务的所有者发送电子邮件。 Ie I do not need to receive an email when I create a task for myself. 即,当我为自己创建任务时,我不需要收到电子邮件。

My mailer's working fine and in my tasks controller, I've tried this: 我的邮件工作正常,在我的任务控制器中,我试过这个:

def create
    @task = Task.new(params[:task])    
    respond_to do |format|
      if @task.save and @task.user_id = current_user.id      
        format.html { redirect_to(tasks_path, :notice => 'Task was successfully created.') }      
        format.xml  { render :xml => @task, :status => :created, :location => @task }        
      elsif @task.save
        format.html { redirect_to(tasks_path, :notice => 'Task was successfully created.') }      
        format.xml  { render :xml => @task, :status => :created, :location => @task }        
        TaskMailer.new_task(@task).deliver      
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
     end
    end
  end

But it's not really working... Any chance of some assistance. 但它并没有真正起作用......有任何帮助的机会。

Replace @task.user_id = current_user.id with @task.user_id == current_user.id . @task.user_id == current_user.id替换@task.user_id = current_user.id @task.user_id == current_user.id

This is not the cause of your error, but you're saving your task two times if @task.user_id != current_user.id . 这不是导致错误的原因,但是如果@task.user_id != current_user.id将任务保存两次。 You could do something like this instead: 你可以这样做:

def create
    @task = Task.new(params[:task])

    respond_to do |format|
      if @task.save
        format.html { redirect_to(tasks_path, :notice => 'Task was successfully created.') }      
        format.xml  { render :xml => @task, :status => :created, :location => @task }        
        TaskMailer.new_task(@task).deliver if @task.user_id != current_user.id  
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
      end
    end
  end
end

Are you not storing the id of the creator? 你没有存储创作者的身份证明吗? If you do, all the data you need is in the model. 如果这样做,您需要的所有数据都在模型中。 Thus just implement an private instance method in Task model. 因此,只需在Task模型中实现私有实例方法。 Something like the following 像下面这样的东西

# Task model
private

def notify_assignee
  if new_record? || user_id_changed? && creator_id != user_id
    TaskMailer.new_task(@task).deliver
  end
end

Call the above method after_save 调用上面的方法after_save

# Task model
after_save :notify_assignee

In case you are not storing the creator_id in the database, create an attribute accesor called :creator_id . 如果您未将creator_id存储在数据库中,请创建名为:creator_id的属性:creator_id

# Task model
attr_accessor :creator_id

In the controller, before saving, do 在控制器中,在保存之前,执行

# Tasks controller
@task.creator_id = current_user.id

and the above method would still work. 而上述方法仍然有效。

You controller re directions would automatically be simplified to 您的控制器重新指示将自动简化为

 if @task.save
    format.html { redirect_to(tasks_path, :notice => 'Task was successfully created.') }      
    format.xml  { render :xml => @task, :status => :created, :location => @task }        
  else
    format.html { render :action => "new" }
    format.xml  { render :xml => @task.errors, :status => :unprocessable_entity }
 end

And also this would be the right way to go as the "business logic" (which in your case is send assignee an email notifying that someone else has assigned a task to them ) would reside in the model. 而且这也是正确的方式,因为“业务逻辑”(在您的情况下是向受让人发送电子邮件,通知其他人已经为其分配了任务 )将驻留在模型中。

It's because of this line, you're using an assignment = instead of the comparision == 这是因为这一行,你使用的是赋值=而不是比较==
Also the current_user should be @current_user instead because you didn't specify it in the method. 此外,current_user应该是@current_user因为您没有在方法中指定它。 (Or you have a method current_user() that you did not post. Then it's fine) (或者你有一个你没有发布的方法current_user() 。那很好)

if @task.save and @task.user_id = current_user.id

It should be 它应该是

if @task.save and @task.user_id == @current_user.id

also imho you should move the mailing-stuff to the Task -model and use an after_save -callback. 你应该将邮件内容移动到Task -model并使用after_save -callback。

也许您需要更改此行: @task.user_id = current_user.id to @task.user_id == current_user.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM