如何在单个操作中更新多个模型? (导轨4)

时间:2016-06-13 21:04:29

标签: ruby-on-rails ruby ruby-on-rails-4

我有4个模型,我有一个非常重要的理由来更新所有模型(如果可能的话,可能在单独的操作中但在1个操作中)。

如果我想更新1个型号,我可以使用

def update
  @modelA = ModelA.find(params[:id]
  respond_to do |format|
      if @modelA.update(modelA_params)
        format.html { redirect_to :back, notice: "" }
        format.json { head :no_content }
      else
        format.json { render json: @modelA.errors, status: :unprocessable_entity }
        format.html { render :show }
      end
    end
end

适当的参数。如果我有配置页面并且我想要更新4个模型(当然是单独的),我该如何更改?每个模型我有4列,我想从模型更新每列的相应属性。就我而言,我使用x-editable gem来更新属性。

  

问题是......有什么黑客攻击吗?像批量更新一样   案例条件或我应该为每个模型创建更新操作   更新它们?(在configuration_controller中)

1 个答案:

答案 0 :(得分:2)

如果您可以将所有4个模型的参数传递给操作,则可以执行以下操作:

assign_attributes

我在这里使用了# app/controllers/configuration_controller.rb def update_model_a @a = ModelA.find(params[:id]) respond_to do |format| if @a.save format.html { redirect_to :back, notice: "" } format.json { head :no_content } else format.json { render json: [@a.errors, @b.errors, @c.errors, @d.errors], status: :unprocessable_entity } format.html { render :show } end end def update_model_b @b = ModelB.find(params[:id]) respond_to do |format| if @b.save format.html { redirect_to :back, notice: "" } format.json { head :no_content } else format.json { render json: [@a.errors, @b.errors, @c.errors, @d.errors], status: :unprocessable_entity } format.html { render :show } end end # ... etc. private def model_a_params params.require(:model_a).permit(:id) # and other permitted params end def model_b_params params.require(:model_b).permit(:id) # and other permitted params end # ... etc. #... same for the other 3 models ,因为您可能需要join

<强> [编辑]

另一方面,如果您想在单独的操作中更新模型,那么只需在控制器中执行4个单独的操作:

public class MainActivity extends Activity {

    Button btn;
    EditText urlInput;
    TextView urlTxt, hashValue, saveLoc, tv4;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button) findViewById(R.id.btn);
        urlInput = (EditText) findViewById(R.id.urlInput);

        urlTxt = (TextView) findViewById(R.id.urlTxt);
        hashValue = (TextView) findViewById(R.id.hashValue);
        saveLoc = (TextView) findViewById(R.id.saveLoc);
        tv4 = (TextView) findViewById(R.id.tv4);

    }


    public void btnClick(View v) throws IOException, NoSuchAlgorithmException {

        Button btn = (Button) v;

        urlTxt.setText("Url entered: " + urlInput.getText());
        String urlCopy = urlInput.getText().toString();

        //problem area ahead ->
        URL uri = new URL(urlCopy);
        URLConnection ec = uri.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(ec.getInputStream(), "UTF-8"));
        String inputLine;
        StringBuilder a = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
            a.append(inputLine);
        in.close();

        int hashedSite = inputLine.hashCode();
        hashValue.setText("Hash Value: " + hashedSite);


        BigInteger bi = BigInteger.valueOf(hashedSite);
        byte[] bytes = bi.toByteArray();
        if ((bytes[0] % 2) == 0) {
            tv4.setText("First byte is an even number: " + bytes[0]);
        } else {
            tv4.setText("First byte is and odd number: " + bytes[0]);
        }


        //        String out = new Scanner(new URL(urlCopy).openStream(), "UTF-8").useDelimiter("\\Z").next();
    }
}