-2

我搭建了一个 Authorization(user_id, code, otherparam1, ...) 并且我想向仅需要代码的 authorizations_controller 添加一个 custom_create 方法,该方法生成其他参数。我需要此方法与 json 一起使用或在其他方法中调用,例如 User.newcustom(code)

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)。任何想法 ?

4

2 回答 2

0

为此,将给定的代码添加到 config/routes.rb

  resources :authorizations do
    collection do 
      post :newcustom
    end
  end
于 2013-05-18T15:03:07.710 回答
0

感谢大家的帮助。

我最终解决了过滤创建函数参数的问题,然后调用了我的自定义创建函数。是的,我作弊:)

于 2013-05-19T17:24:49.063 回答