嵌套表单:从另一个对象复制fields_for

时间:2016-06-22 08:49:17

标签: ruby-on-rails nested-forms

为Person和Post模型之间的has_may关系实现了nested_form

["1"=>"2016-06-22 09:18:45","2"=>"2016-06-22 04:18:45", "3"=>"2016-06-22 01:18:45"]

效果很好。现在有按钮"复制人"通过填充前一个人的所有属性重定向到新人页面。除了该人的帖子之外,人口的所有字段都被填充。以下是呈现帖子的视图代码:

public void login(View view) {
    switch (view.getId()) {

        case R.id.signin:
            String employCod = editTextUserCode.getText().toString();
            String branchCod = editTextBranchCode.getText().toString();
            String password = editTextPassword.getText().toString();
            onlogin(employCod, branchCod, password);

            break;

    }
}

private void onlogin(final String employCod, final String branchCod, final String password) {

    class LoginAsync extends AsyncTask<String, String, String> {
        /**
         * Before starting background thread Show Progress Dialog
         */
        boolean failure = false;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(LoginActivity.this);
            pDialog.setMessage("Attempting login...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... args) {

            // TODO Auto-generated method stub
            // Check for success tag


            String companyCode = "VO";
            String loginBranch = "VO02";

            try {
                // Building Parameters
                List<NameValuePair> params = new ArrayList<>();
                params.add(new BasicNameValuePair("companyCode", companyCode));
                params.add(new BasicNameValuePair("employeeCode", employCod));
                params.add(new BasicNameValuePair("empBranch", branchCod));
                params.add(new BasicNameValuePair("passWord", password));
                params.add(new BasicNameValuePair("loginBranch", loginBranch));
                Log.d("request!", "starting");
                // getting product details by making HTTP request
                JSONObject json = jsonParser.makeHttpRequest(
                        LOGIN_URL, "POST", params);


                // check your log for json response
                Log.d("Login attempt", json.toString());

                // json success tag
                errflag = json.getString(TAG_SUCCESS);
                if (errflag.equalsIgnoreCase("S")) {
                    Log.d("Login Successful!", json.toString());
                    Intent i = new Intent(LoginActivity.this, MainActivity.class);
                    finish();
                    String employName = json.getString("empName");
                    i.putExtra("empName", employName);
                    startActivity(i);

                    return json.getString(TAG_MESSAGE);
                } else {
                    Log.d("Login Failure!", json.getString(TAG_MESSAGE));
                    return json.getString(TAG_MESSAGE);

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;

        }


        /**
         * After completing background task Dismiss the progress dialog
         * **/

        protected void onPostExecute(String file_url) {
            // dismiss the dialog once product deleted
            pDialog.dismiss();
            if (file_url != null) {
                Toast.makeText(LoginActivity.this, file_url, Toast.LENGTH_LONG).show();


            }

        }

    }
    LoginAsync la = new LoginAsync();
    la.execute(employCod, branchCod, password);

控制器:

class Person
  has_many :posts
  accepts_nested_attributes_for :posts
end

如果一个人有3个帖子,它应该填充人的所有字段,并且在复制时也应显示3个帖子。上一个person对象在<%= f.fields_for :posts do |ff| %> <%= render 'post_fields', ff: ff %> <% end %>

视图中可用

为了呈现这些帖子我需要做哪些改变?

1 个答案:

答案 0 :(得分:1)

请考虑根据previous_person的属性

创建新人和他们的帖子
def copy_person
  previous_person = Person.find(params[:id])
  @person = Person.create!(previous_person.attributes.except("id"))
  previous_person.posts.each do |old_posts|
    @person.posts.create!(old_posts.attributes.except("id"))
  end
end

现在,您已使用所有帖子从上一个复制了新创建的人员。此外,您可以使用dup方法创建人员副本

def copy_person
  previous_person = Person.find(params[:id])
  @person = previous_person.dup
  @person.save!
  previous_person.posts.each do |old_posts|
    @person.posts.create!(old_posts.attributes.except("id"))
  end
end