Ruby自定义创建方法
问题描述:
我搭建了一个授权(user_id,code,otherparam1,...),并且我想将一个custom_create方法添加到authorizations_controller,它只需要一个代码,该方法生成其他参数。我需要这个方法使用JSON工作或其他方法,比如User.newcustom(代码)Ruby自定义创建方法
def newcustom
case request.method
when :post #post
code = params[:code]
if(code and code != '')
...
respond_to do |format|
if @authorization.save
format.html { redirect_to @authorizations, notice: 'Authorization was successfully created.' }
format.json { render json: @authorizations, status: :created, location: @authorization }
else
format.html { render action: "new" }
format.json { render json: @authorization.errors, status: :unprocessable_entity }
end
end
else #get
respond_to do |format|
format.html
end
end
叫这里是我的newcustom.html.erb
<%= form_tag(newcustom_authorizations_path, :method => :post) do %>
<div class="field">
<%= text_field_tag :code %>
</div>
<div class="actions">
<%= submit_tag('Get tokens') %>
</div>
<% end %>
但它不通过JSON工作, 表格。并调用newcustom(:code => code)方法会引发太多参数(1代表0)。任何想法 ?
答
该附加给定的代码到config/routes.rb中
resources :authorizations do
collection do
post :newcustom
end
end
+0
已经将我的routes.rb – Lex
答
感谢大家的帮助。
我最终解决了我的问题,过滤create函数的参数,然后调用我的customcreate函数。 并且我是CHEAT :)
你在哪里叫newcustom? –
这不是PHP,老兄!您需要RESTful操作。 – MikDiet
向我们显示错误消息/堆栈跟踪和错误消息中引用的代码。 –