表单中的 Rails 复选框决定控制器中的创建操作?

发布于 2024-09-05 19:48:26 字数 1051 浏览 1 评论 0原文

我正在尝试在创建表单的底部添加一个选择框,以决定是否从控制器运行操作...这是否有意义?

基本上,每当创建新项目时,应用程序都会在 FreeagentCentral 中创建一个项目:

def create
    @company = Company.find(params[:kase][:company_id])
    @kase = @company.kases.create!(params[:kase])
    
    respond_to do |format|
        params[:send_to_freeagent] ? @kase.create_freeagent_project(current_user)
        
        #flash[:notice] = 'Case was successfully created.'
        flash[:notice] = fading_flash_message("Case was successfully created.", 5)
        
        format.html { redirect_to(@kase) }
        format.xml  { render :xml => @kase, :status => :created, :location => @kase }
    end
  end

在我的表单中,我有:

<%= check_box_tag :send_to_freeagent, 1 %> Create project in Freeagent?

我希望发生的是,如果选中该选择框,则项目将发送到 Freeagent。如果没有,案例只会像平常一样在本地创建,但不会发送 Freeagent 数据。

如果我使用上面的代码,我会收到异常捕获错误:

SyntaxError in KasesController#new
controllers/kases_controller.rb:114: syntax error, unexpected '\n'

知道我做错了什么吗?

I am trying to add a select box to the base of my create form that decides if an action runs from the controller...if that makes any sense?

Basically the application creates a project in FreeagentCentral whenever a new project is made:

def create
    @company = Company.find(params[:kase][:company_id])
    @kase = @company.kases.create!(params[:kase])
    
    respond_to do |format|
        params[:send_to_freeagent] ? @kase.create_freeagent_project(current_user)
        
        #flash[:notice] = 'Case was successfully created.'
        flash[:notice] = fading_flash_message("Case was successfully created.", 5)
        
        format.html { redirect_to(@kase) }
        format.xml  { render :xml => @kase, :status => :created, :location => @kase }
    end
  end

and within my form I have:

<%= check_box_tag :send_to_freeagent, 1 %> Create project in Freeagent?

What I would like to happen, is if the select box is checked the project is sent to Freeagent. If not, the case just gets created locally as normal but without the Freeagent data being sent.

If I use the above code, I get an exception caught error:

SyntaxError in KasesController#new
controllers/kases_controller.rb:114: syntax error, unexpected '\n'

Any idea what I am doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过潦 2024-09-12 19:48:26

我会用

def create
   @company = Company.find(params[:kase][:company_id])
   @kase = @company.kases.create!(params[:kase])

   @kase.create_freeagent_project(current_user) if params[:send_to_freeagent].to_bool

   respond_to do |format|       
      # ...
   end
end

I'd use

def create
   @company = Company.find(params[:kase][:company_id])
   @kase = @company.kases.create!(params[:kase])

   @kase.create_freeagent_project(current_user) if params[:send_to_freeagent].to_bool

   respond_to do |format|       
      # ...
   end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文