简体   繁体   English

如何在Rails应用程序中将条件重定向写入模型的create方法?

[英]How do I write a conditional redirect into a model's create method, in a Rails app?

I'm allowing users to create a 'Question' from different pages in the app, and want them to be redirected to the right page after the new question is saved. 我允许用户从应用程序的不同页面创建一个“问题”,并希望在保存新问题后将他们重定向到正确的页面。 Since one page has a params[:id] and one doesn't, I thought I'd use that to differentiate between the different redirects. 由于一页包含params [:id],而另一页没有params [:id],所以我认为我可以使用它来区分不同的重定向。

Here's what I was trying to do in my Questions controller: 这是我在Questions控制器中尝试执行的操作:

respond_to do |format|  
if @question.save
    if params[:id].nil?
      format.html {redirect_to :controller => 'home', :action => 'show', :username => question.directed_to}  
    else
      format.html {redirect_to :controller => 'mentions', :action => 'show', :id => question.topic}
    end    
else
    ...
end

Thanks very much, I'm still new to this. 非常感谢,我还是新手。

Relying on params beings nil is super awkward. 依靠参数众生nil超级笨拙。 You have control of the forms being submitted, so just sent an extra parameter yourself which identifies where it's coming from. 您可以控制所提交的表单,因此您自己发送了一个额外的参数,即可确定表单的来源。

A few other things: 其他一些事情:

If you're only responding with html, don't bother doing the respond_to block. 如果您仅使用html进行响应,则不必理会response_to块。

if @question.save
  redirect_to :controller => 'home', :action => 'show', :username => question.directed_to
else
  redirect_to :controller => 'mentions', :action => 'show', :id => question.topic
end

@question.save doesn't run validations. @question.save不运行验证。 It's pretty unusual that you'd want to have a create action that doesn't run validations. 您想要具有不运行验证的create动作是非常不寻常的。 You should probably having something like: 您可能应该有以下内容:

def create
  @question = Question.new(params[:question])
  @question.save!
  flash[:notice] = "Question saved successfully"
  # Do your redirects here
rescue ActiveRecord::RecordInvalid
  flash[:error] = "There were errors saving your question"
  render :action => :new
end

暂无
暂无

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

相关问题 如何在Rails模型中包含gem的class方法? - How do I include a gem's class method in a Rails model? 如何为 Rails 中的嵌套资源编写此条件语句? - How do I write this conditional statement for a nested resource in rails? 在Rails中,我试图将属性传递给Model的“ new”方法,然后传递给“ create method”。 最有效的方法是什么? - In Rails, I'm trying to pass an attribute to a Model's “new” method, then to the “create method”. What's the most efficient way to do this? 如何在Ruby on Rails模型中进行条件验证? - How can I do conditional validation in a Ruby on Rails Model? Rails:如何为执行重定向的路由编写规范? - Rails: How do I write a spec for a route that does a redirect? 我如何创建从WordPress(驻留在wsynth上)到Heroku托管的Rails应用的301重定向? - How do I create 301 redirect from WordPress (hosted on wsynth) to a Rails app hosted on Heroku? Rails如何在模型的方法内部建立条件 - Rails How make a conditional inside a method on the model 如何调整条件以在 Rails 中为实例方法传递符号? - How do I adjust conditional to pass a symbol for an instance method in Rails? Ruby on Rails:如何在模型中创建初始化方法,还是应该在控制器中设置所有默认参数? - Ruby on Rails: how do I create an initializer method in the model or should I set all the default params in a controller? 如何在Rails中基于范围的多租户应用程序中创建可跨公司的代理模型 - How do I create an Agent model that can spans across Companies in a scope based multi tenancy app in Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM